I'm trying to make a simple guessing game in Python. I, the user, has three guesses to guess the correct number. I tried to do it by myself and I used the if statement, while in the correct solution, while loop should be used. My solution works quite well but when I guess the correct number in the first or second try, I receive an error that inputs are missing for second/third number, please see code below. I understand that 'break' can only be used in a while loop. Is there a way to make this work using the if statement or is this only solvable by using the while loop? Beginner 'coder' here, please have patience. Thank you!
correct = 3
first_number = int(input('Guess: '))
if first_number == correct:
print('You win!')
elif first_number != correct:
second_number = int(input('Try again: '))
if second_number == correct:
print('You win, second guess!')
elif second_number != correct:
third_number = int(input('Last guess: '))
if third_number == correct:
print('Finally!')
elif third_number != correct:
print('You lose!')
CodePudding user response:
You can stop the code execution using sys.exit()
after importing the sys module import sys
import sys
# your code
#Let's say you want to "break" or stop the execution here...
sys.exit()
However, this is not an efficient way to program anything. I would recommend you to use while loop and a break statement. (or even a for loop)
#Example
import random
correct = random.randint(0,10)
chances = 3
while chances >0:
chances -=1
choice = int(input("Guess: "))
if choice == correct:
print(f"Correct! You had {chances} chances left!")
else:
print(f"Wrong! You have {chances} chances left!")
The randint from random module helps you find a random integer number.
CodePudding user response:
A reasonable structure for this would be to use a for loop to manage the maximum number of allowable attempts. Something like this:
computer_guess = 3
max_tries = 5
for i in range(max_tries):
if int(input('Your guess: ' if i == 0 else 'Try again: ')) == computer_guess:
print('Correct!')
break
else:
print('You ran out of guesses')
CodePudding user response:
Python 3.8. We use the walrus
operator in a while
loop.
Before:
correct = 3
first_number = int(input('Guess: '))
if first_number == correct:
print('You win!')
elif first_number != correct:
second_number = int(input('Try again: '))
if second_number == correct:
print('You win, second guess!')
elif second_number != correct:
third_number = int(input('Last guess: '))
if third_number == correct:
print('Finally!')
elif third_number != correct:
print('You lose!')
After:
correct = 3
while (first_number := int(input('Guess: ')) is not correct):
print('Try again!')
else:
print('You win!')
CodePudding user response:
You can easily do this using only if-elif-else statements. A while loop, though efficient, is not necessary:
correct = 3
first_number = int(input('Guess: '))
if first_number == correct:
print('You win!')
elif first_number != correct:
second_number = int(input('Try again: '))
if second_number == correct:
print('You win, second guess!')
elif second_number != correct:
third_number = int(input('Last guess: '))
if third_number == correct:
print('Finally!')
elif third_number != correct:
print('You lose!')
We can use nested if-statements, so that if the user gets the answer wrong and we go into the elif part of the chain, it will trigger the next set of questions, and so on.
Solution with while loop:
correct = 3
tries = 3
while tries != 0:
guess = int(input("Please guess"))
if guess == correct :
print("YOU GOT IT :DDD")
break
else:
print("Wrong. Please try again")
tries -= 1
if tries == 0:
print("You lost :(")
We start with 3 tries. In each iteration of the loop (while the user has guesses), we give the user a chance to guess correctly. If they guess right, we congratulate them and exit the loop. If they guess incorrectly, we will tell them they got it wrong and decrement the number of tries they have. Finally, if they exit the loop and have no more tries left, we tell them they lost.