I am new to python and i want to make something that asks your either: Yes or No. If the user says yes it does something and if they say no it asks the question again until they say yes. How do i do this? Thanks in advance!
Asked
Active
Viewed 44 times
-3
-
Can you show what you've tried so far? – ShlomiF Jun 29 '21 at 18:24
3 Answers
0
A simple example is:
answer = input('Your question. Please answer yes or no.')
while answer != 'yes':
answer = input('Your question. Please answer yes/no.')
# executes only once answer == 'yes'
something()

StrangeSorcerer
- 304
- 5
- 11
0
while True:
answer = input('Question: ')
if answer == 'Yes':
# do something
break

Harshil Modi
- 397
- 3
- 8
- 15

Geom
- 1,491
- 1
- 10
- 23
0
A simple while loop with some assertions...
answer = input('Do you agree? y/n')
assert answer in ['y', 'n'], 'Please answer only with a "y" or a "n"'
answer = input('Do you agree? y/n')
while answer == 'n':
answer = input('Do you agree? y/n')
assert answer in ['y', 'n'], 'Please answer only with a "y" or a "n"'
# If we're here the user has finally answered "y"
do_something()

ShlomiF
- 2,686
- 1
- 14
- 19