1
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?

asked Apr 25, 2021 at 18:35
1
  • Have you considered a while loop or are your purely interested in accomplishing this without a loop structure? Commented Apr 25, 2021 at 18:39

2 Answers 2

1
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
answered Apr 25, 2021 at 18:42
Sign up to request clarification or add additional context in comments.

Comments

0

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!

answered Apr 25, 2021 at 18:51

2 Comments

When you are using the break statements, you don't need to add a condition on the while loop, and vice versa. Check out the two options in my answer.
@mcsoini I know. I just wanted to put it in a way that a beginner would understand, using the code that they provided. I wanted the question asker to understand the code I wrote, and I wanted to use the exact code they used but with a while loop and changing nothing at all.

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.