Help for the simple terminal in phyton | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
+ 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")

6th Aug 2023, 12:27 PM
Mohammad Razavi
Mohammad Razavi - avatar
17 Answers
+ 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
6th Aug 2023, 2:19 PM
Lothar
Lothar - avatar
+ 4
I would put if statements inside the loop
6th Aug 2023, 1:11 PM
Lamron
Lamron - avatar
+ 3
The user must capitalize his or her answer before the code can validate it
6th Aug 2023, 12:32 PM
Aweneg Rooney
Aweneg Rooney - avatar
+ 3
Lothar Thank you very much! Your answer helped me a lot! 🙏👍
6th Aug 2023, 2:17 PM
Mohammad Razavi
Mohammad Razavi - avatar
+ 3
Here is a working example of my suggestion: https://code.sololearn.com/c5DzR3BlJHT8/?ref=app
6th Aug 2023, 2:24 PM
Keith
Keith - avatar
+ 2
Aweneg Rooney Even the user entered the capitalized word it says "please enter Y or N"
6th Aug 2023, 12:34 PM
Mohammad Razavi
Mohammad Razavi - avatar
+ 2
You could add answer=answer.upper() after your input() statement.
6th Aug 2023, 1:21 PM
Keith
Keith - avatar
+ 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
6th Aug 2023, 1:50 PM
Mohammad Razavi
Mohammad Razavi - avatar
+ 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.
6th Aug 2023, 1:52 PM
Mohammad Razavi
Mohammad Razavi - avatar
+ 2
Keith Driscoll Thanks for your kindness sir ! 🙏
6th Aug 2023, 2:35 PM
Mohammad Razavi
Mohammad Razavi - avatar
+ 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😂😂
6th Aug 2023, 11:10 PM
Olasehinde Michael
Olasehinde Michael - avatar
+ 2
Hello bro
7th Aug 2023, 9:34 AM
devansh Pandey
devansh Pandey - avatar
7th Aug 2023, 4:52 PM
Eaz Mahodi Sagor
Eaz Mahodi Sagor - avatar
+ 1
Olasehinde Michael Thank you for your code. It Helped me a lot! 🙏
7th Aug 2023, 6:08 PM
Mohammad Razavi
Mohammad Razavi - avatar
0
Hi can we be friends 🙂 ☺️ 😊 pls Idond have any friend 🥺🥺🥺
7th Aug 2023, 4:58 PM
Amro MahmoudBerbara
Amro MahmoudBerbara - avatar
0
Eaz Mahodi Sagor Thank you sir Your code Helped me a lot! 🙏
7th Aug 2023, 6:08 PM
Mohammad Razavi
Mohammad Razavi - avatar
0
while True: command = input("Enter a command: ") if command == "exit": print("Exiting the terminal.") break print(f"You entered: {command}")
8th Aug 2023, 11:09 AM
Vaibhav Singh
Vaibhav Singh - avatar