Python Forum
Complex Jumping In Code
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Complex Jumping In Code
#10
(Mar-15-2020, 07:52 AM)WhamDie Wrote: I Wanna Do Something Do Repeat The Iteration, Like When The User Enter A String With Space Or A Digit, The User Is Prompted Again To Enter The Same Value. Using break Will Stop The Whole Program, Which I Definitely Not Wanna Do. What Should I Do? Please Help
To show one way,all code in global namespace make it hard to do logic as repeat and ask question again.
So here a function for the first part,return will break all as it goes our of function.
When this part is finish it will not mess with rest of code,as the point with a function is that code inside is isolated.
def words():
    word_lst = []
    word_count = int(input("How many words do you want to enter: "))
    while True:
        word = input("Enter a word: ")
        if any(c.isspace() for c in word):
            print("Enter a word not a sentence,try again")
        elif not word.isalpha():
            print("Use alphabets only,try again")
        else:
            word_lst.append(word)
            if len(word_lst) >= word_count:
                return word_lst
Test.
Output:
>>> word_list = words() How many words do you want to enter: 2 Enter a word: 99 Use alphabets only,try again Enter a word: *.. Use alphabets only,try again Enter a word: hello world Enter a word not a sentence,try again Enter a word: hello Enter a word: world >>> word_list ['hello', 'world']
Reply


Messages In This Thread
Complex Jumping In Code - by WhamDie - Mar-15-2020, 07:52 AM
RE: Complex Jumping In Code - by Larz60+ - Mar-15-2020, 08:06 AM
RE: Complex Jumping In Code - by buran - Mar-15-2020, 08:16 AM
RE: Complex Jumping In Code - by WhamDie - Mar-15-2020, 09:58 AM
RE: Complex Jumping In Code - by buran - Mar-15-2020, 10:04 AM
RE: Complex Jumping In Code - by ndc85430 - Mar-15-2020, 10:36 AM
RE: Complex Jumping In Code - by WhamDie - Mar-15-2020, 10:43 AM
RE: Complex Jumping In Code - by ndc85430 - Mar-15-2020, 06:59 PM
RE: Complex Jumping In Code - by buran - Mar-15-2020, 07:03 PM
RE: Complex Jumping In Code - by snippsat - Mar-15-2020, 10:03 PM

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020