0

How could this while loop work if the needed input were not submitted?

#Dice Rolling Stimulator Beginning def decor(x): print(len(x) * "*") print(x) print(len(x) * "*") welcoming = "Welcome to this dazzling dice rolling stimulator!" decor(welcoming) answer = input("\nAre you ready to spin? Yes/No") answer.lower() #I have problem with the following lines. I tried to use the return function to avoid infinite loops but understood that is was not possible without a function, so I added one but it is unsatisfactory. The code is supposed to return the statement "Are you ready to spin?! Yes/No" if either "yes" or "no" was not submitted. Could use some help. Thanks beforehand. def ready(answer): while answer != "yes" or "no": return"\nAre you ready to spin?! Yes/No" print(ready(answer))

10th Apr 2017, 9:24 PM
Annerb
Annerb - avatar
5 Respostas
+ 1
Here is your code with the return function loop you wanted, but it is strange. Your return loop is basically a wierd way to do a recursive function (a function that calls itself) .. check the function at the end, I just wrote it def decor(x): print(len(x) * "*") print(x) print(len(x) * "*") welcoming = "Welcome to this dazzling dice rolling stimulator!" decor(welcoming) answer = input("\nAre you ready to spin? Yes/No") answer.lower() def ready(answer): while answer != "yes" or "no": return ready(input("\nAre you ready to spin?! Yes/No")) print(ready(answer)) -------------- def ready(answer): if answer=="yes": ready(input("Are you ready to spin? Yes/No")) else: return
10th Apr 2017, 11:53 PM
LordHill
LordHill - avatar
+ 1
Wouldn't it be easier to skip the function with a wierd return and just have a while loop? def decor(x): print(len(x) * "*") print(x) print(len(x) * "*") welcoming = "Welcome to this dazzling dice rolling stimulator!" decor(welcoming) running=True while running==True: answer = input("\nAre you ready to spin? Yes/No") answer.lower() if answer=="yes": continue else: break
10th Apr 2017, 11:55 PM
LordHill
LordHill - avatar
+ 1
Thanks, I really did not want to add a function but when I googled a solution to my problem it said that to avoid having return out of the function error I must make a function. Thanks again
11th Apr 2017, 7:31 AM
Annerb
Annerb - avatar
+ 1
Yeah I agree, for loops are pretty cool
11th Apr 2017, 10:08 AM
Annerb
Annerb - avatar
0
no problem. check into loops. while loops are generally used to keep a program running. For Loops are my favorite and are used for all kind of awesome stuff. for loops take a minute to understand, but once you do they are amazing
11th Apr 2017, 9:43 AM
LordHill
LordHill - avatar