print("What's your name?")
name = input('Answer: ')
while False:
if name == 'joshua':
print("you're right")
else:
print("you're wrong")
input('the end\n')
- 46,905
- 10
- 55
- 92
- 35
- 3
-
set a boolean flag and make that the condition of your while loop. i.e `incorrectAnswer = true; while incorrectAnswer: if name == 'joshua': incorrectAnswer = false` – Jeremy Fisher Feb 20 '20 at 19:16
-
3`while False:` will never execute at all. – Barmar Feb 20 '20 at 19:17
-
Note: Avoid infinite loops. Add another condition to exit the loop. In this case maybe if guesses > 20. – Brian Feb 20 '20 at 19:27
5 Answers
- With
while False:, you never get inside the loop at all - Your problem with the loop body isn't getting back to the top; it is that you haven't specified a way to exit the loop.
- 46,905
- 10
- 55
- 92
You can set a boolean variable outside of the loop which will run basically forever until the answer is correct. You need to ask for input again in each iteration:
print("What's your name?")
unanswered = True
while unanswered:
name = input('Answer: ')
if name == 'joshua':
unanswered = False
print("you're right")
else:
print("you're wrong")
input('the end\n')
- 11,690
- 6
- 30
- 49
Use while True: to loop infinitely, then break out of the loop when the answer is correct.
while True:
print("What's your name?")
name = input('Answer: ')
if name == 'joshua':
print("you're right")
break
else:
print("you're wrong")
- 669,327
- 51
- 454
- 560
Use break and continue inside your while loop. Control flow
while True:
if name != "Joshua":
continue
break
- 570
- 3
- 16
That loop will never enter because its condition is always False. You'll need an extra variable to keep track. Start off by setting correct = False above the loop and changing your loop condition to not correct. Then, if they are correct, set correct = False.
You could also break on a right answer with True for the loop condition, but since I'm guessing you are new to programming, learning more traditional control flow would probably be better.
- 2,272
- 1
- 15
- 22