+ 2
Help for the simple terminal in phyton
Hello I want to create a simple terminal to ask the user "Are you ready?" 1- If user entered "Y" then terminal show the question 2- If user entered "N" then terminal finish 3 - If user entered any other word terminal show "Please enter Y or N" and ask the question again. I write it like below but it always says the "Please enter Y or N" print("So, Are you ready for the game?") print("Y=Yes N=No") answer = input() while answer != "Y" or answer != "N": print("Please enter Y or N") answer = input() if answer == "Y": print("Great! Lets start") if answer == "N": print("Come back when you were ready")
17 odpowiedzi
+ 8
Mohammad Razavi ,
> the issue is the current logical expression, so lets fix this.
here you can see how the evaluation of the expression is:
if input => Y
answer = 'Y', answer != "Y" = False, answer != "N" = True, answer != "Y" or answer != "N" = True
if input => N
answer = 'N', answer != "Y" = True, answer != "N" = False, answer != "Y" or answer != "N" = True
if input = X
answer = 'X', answer != "Y" = True, answer != "N" = True, answer != "Y" or answer != "N" = True
since we are using a logical *or*, the result of the expression will be True if one of the parts is True.
> so what ever character we input, the complete expression will be true (which is while True: => infinite loop)
> to fix it we can use this:
while answer not in 'YN':
...
or this:
while not (answer == 'Y' or answer == 'N'):
...
https://code.sololearn.com/cAhdevvav4pF/?ref=app
+ 4
I would put if statements inside the loop
+ 3
The user must capitalize his or her answer before the code can validate it
+ 3
Lothar
Thank you very much!
Your answer helped me a lot! 🙏👍
+ 3
Here is a working example of my suggestion:
https://code.sololearn.com/c5DzR3BlJHT8/?ref=app
+ 2
Aweneg Rooney
Even the user entered the capitalized word it says "please enter Y or N"
+ 2
You could add answer=answer.upper() after your input() statement.
+ 2
Keith Driscoll
When i change the answer = input() to answer = input().upper it dosen't work and show the "Please enter Y or N" again
+ 2
Lamron
Thanks! It worked but still have problem.
When i put the if statements inside the while loop user have to press Y or N twice.
First time terminal shows "Please enter Y or N" but at second try it works well.
+ 2
Keith Driscoll
Thanks for your kindness sir ! 🙏
+ 2
It is best you use a loop for this so that your code does not look too messy and you are able to call back later
while True :
response1 = input("Y=Yes N=No : ")
if response1 == "Y" :
print("Great! Lets start")
elif response1 == "N" :
print("Come back when you were ready")
break
else :
print("Please enter a valid input :(")
So this loops keeps asking tho😂😂
+ 2
Hello bro
+ 1
Olasehinde Michael
Thank you for your code.
It Helped me a lot! 🙏
0
Hi can we be friends 🙂 ☺️ 😊 pls Idond have any friend 🥺🥺🥺
0
Eaz Mahodi Sagor
Thank you sir
Your code Helped me a lot! 🙏
0
while True:
command = input("Enter a command: ")
if command == "exit":
print("Exiting the terminal.")
break
print(f"You entered: {command}")