if explication == "y":
print("The game is very simple, the programe generate a random number between 0 and 100 and your" +
" objective is to guess it, if you type a number lower than the generated number i'll tell you" +
" and the same will happen if you type a bigger number and there's a score if you guess wrong" +
" it will decrease and when the score reach 0 you loose, that's all enjoy the game!")
if explication == "n":
print("Great then we'll go straight to having fun!")
if explication != "n" and explication != "y":
explication = input("please choose y or n: ")
So i started learning python and i wanted to do a simple fun program, and here i wanted to ask the user if he needed explication of the game that was simple to do, but i also wanted to make him choose another time if he miss clicked or he just wanted to type other things so i made the 3rd if statement, i also wanted it to repeat so if he keeps typing things other than y and n the program will always send "please choose y or n" until he he types either y or n is there a way to do this?
-
Have you considered a while loop or are your purely interested in accomplishing this without a loop structure?Cowboy– Cowboy2021年04月25日 18:39:14 +00:00Commented Apr 25, 2021 at 18:39
2 Answers 2
explication = None
# keep asking until we get "y" or "n"
while explication not in ["y", "n"]:
# retrieve input
explication = input("please choose y or n: ")
# check whether it's y or n
if explication == "y":
print("The game is very simple...")
elif explication == "n":
print("Great then we'll go straight to having fun!")
# if the input is neither y nor n, the program ends up here
# and loops again
Alternative:
# keep looping unconditionally
while True:
# retrieve input
explication = input("please choose y or n: ")
if explication == "y":
# print and break
print("The game is very simple...")
break # <- this gets us out of the while loop
elif explication == "n":
print("Great then we'll go straight to having fun!")
break
Comments
Here is a simple way to write the code
explication = None
while(explication != "n" and explication != "y"):
explication = input("please choose y or n: ")
if explication == "y":
print("The game is very simple, the programe generate a random number between 0 and 100 and your" +
" objective is to guess it, if you type a number lower than the generated number i'll tell you" +
" and the same will happen if you type a bigger number and there's a score if you guess wrong" +
" it will decrease and when the score reach 0 you loose, that's all enjoy the game!")
break
if explication == "n":
print("Great then we'll go straight to having fun!")
break
#Code to start the game after this comment
Explanation
We set explication to None as the explication variable has to be initialized first. The while loop runs while the input is not y or n. If it is y, we explain the game and then leave the loop using break. If it is n, we skip the explication and break from the loop. After you break from the loop you can put the code for the game!