- 1

Help with python

I have this code if statement code and I want to add a while loop so that if the user input wrongly it will tell them to go back to the previous question. but I don't know how. ex add=input(" You will have to add these documents together. if you choose the left side type LEFT if you choose the right side type RIGHT) if add.upper()=='LEFT': LEFT=input('Nice choice so we will open the documents using .... and another choice selections) elif add.upper()=='RIGHT': RIGHT=input() else: print("Wrong choice please try again") (and then it should bring out the "add" question here for them to try again)

3rd Feb 2022, 6:06 AM
Chioma Chieke
20 Antworten
+ 2
Place the whole code within while(True): For it to go infinitely. If there is a stopping condition, then put that in place of True
3rd Feb 2022, 7:11 AM
Adi Nath Bhawani
Adi Nath Bhawani - avatar
0
Chioma Chieke Set up your while loop with an exit condition, then place your code within it. Your code will keep repeating until the exit condition is met.
3rd Feb 2022, 6:38 AM
Rik Wittkopp
Rik Wittkopp - avatar
0
it's not working. I have nested if statements. and I want each to go to the question in a case where the user input something wrong
3rd Feb 2022, 8:36 AM
Chioma Chieke
0
while(True): add=input(" You will have to add these documents together. if you choose the left side type LEFT if you choose the right side type RIGHT") if add.upper()=='LEFT': LEFT=input('Nice choice so we will open the documents using .... and another choice selections') elif add.upper()=='RIGHT': RIGHT=input() else: print("Wrong choice please try again")
3rd Feb 2022, 8:45 AM
Adi Nath Bhawani
Adi Nath Bhawani - avatar
0
You had missed putting the end quotes for every sentence
3rd Feb 2022, 8:46 AM
Adi Nath Bhawani
Adi Nath Bhawani - avatar
0
my code is so many I would have pasted it here
3rd Feb 2022, 8:51 AM
Chioma Chieke
0
from ast import If while True:             game_start=input('Do you want to play this Text Guessing Game?: (yes/no)')     print('-----PLEASE FOLLOW THE CAPITAL LETTER CHOICES, AND INPUT EXACTLY AS SHOWN IN THE STORYY---------')     if game_start.upper().strip()=='YES':       two_path=  input('You woke up from sleep only to see yourself at a two direction quiet paths with two signs, \nthe left sign says Dont Go Right, and the Right said Left is No Safe,\n which diretion will you follow? RIGHT or LEFT? ')     if two_path.upper()=='RIGHT':         right_choice=input('you walk through the right path and you noticed that it is leading to a thick forest with a light illuminating from a house at the end of the forest,\n but you suddenly heard human voices coming from the sides of the forest, would you chose to FOLLOW THE VOICES or CONTINUE MOVING? ')           if right_choice.upper().strip()=='FOLLOW THE VOICES':             kids=input('You follow the voices, only to see two strange looking hungry children\n you ask
3rd Feb 2022, 8:53 AM
Chioma Chieke
0
something like this.I want it to go back in a case they didn't make the choice shown
3rd Feb 2022, 8:54 AM
Chioma Chieke
0
Then wherever you find they did not enter the right choice, write continue
3rd Feb 2022, 8:56 AM
Adi Nath Bhawani
Adi Nath Bhawani - avatar
0
while(True): add=input(" You will have to add these documents together. if you choose the left side type LEFT if you choose the right side type RIGHT") if add.upper()=='LEFT': LEFT=input('Nice choice so we will open the documents using .... and another choice selections') elif add.upper()=='RIGHT': RIGHT=input() else: print("Wrong choice please try again") continue
3rd Feb 2022, 8:57 AM
Adi Nath Bhawani
Adi Nath Bhawani - avatar
0
What the keyword 'continue' does is that it leaves whatever code that is written below the keyword and returns back to the loop statement
3rd Feb 2022, 9:00 AM
Adi Nath Bhawani
Adi Nath Bhawani - avatar
0
okay but it breaks the program instead of returning back
3rd Feb 2022, 9:04 AM
Chioma Chieke
0
from ast import If while True:     game_start=input('Do you want to play this Text Guessing Game?: (yes/no)')     if game_start.upper().strip()=='YES':       two_path=  input('You woke up from sleep only to see yourself at a two direction quiet paths with two signs, \nthe left sign says Dont Go Right, and the Right said Left is No Safe,\n which diretion will you follow? RIGHT or LEFT? ')     elif game_start.upper().strip()=='NO':         print('Oh thats bad, you should have just tried')         else:         print('Wrong choice please try again')           pass
3rd Feb 2022, 9:07 AM
Chioma Chieke
0
like this?it doesn't return still
3rd Feb 2022, 9:08 AM
Chioma Chieke
0
Not pass, it was continue 😅 I confused with the terms
3rd Feb 2022, 9:08 AM
Adi Nath Bhawani
Adi Nath Bhawani - avatar
0
okay okay.now I understand
3rd Feb 2022, 9:09 AM
Chioma Chieke
0
it works thankssss
3rd Feb 2022, 9:09 AM
Chioma Chieke
0
Welcome 😉
3rd Feb 2022, 9:10 AM
Adi Nath Bhawani
Adi Nath Bhawani - avatar
0
Please read the comments inside so that you understand the code well, because sololearn doesn't have a terminal the input is written when a white box is showed to user. As you want the program to run dynamically, while sololearn offer coding ground only which will allow the program to run partially dynamic which means your code will run but not allowing user to input data and run recursively. https://code.sololearn.com/cu3V9WA2A48f/?ref=app
4th Feb 2022, 2:34 PM
Rufai Limantawa
0
I think the best way to solve this problem is to put every thing in a while loop.The header should be like this: while add.upper() !='LEFT' and add.upper()!='RIGHT':
5th Feb 2022, 1:24 AM
Gp Ugbe