(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 HelpTo 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_lstTest.
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']