- 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)
20 Respuestas
+ 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
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.
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
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")
0
You had missed putting the end quotes for every sentence
0
my code is so many I would have pasted it here
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
0
something like this.I want it to go back in a case they didn't make the choice shown
0
Then wherever you find they did not enter the right choice, write continue
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
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
0
okay but it breaks the program instead of returning back
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
0
like this?it doesn't return still
0
Not pass, it was continue 😅
I confused with the terms
0
okay okay.now I understand
0
it works thankssss
0
Welcome 😉
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
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':