I am working on a python project "Guess that number" where the computer chooses some random number from the given range and the user has to guess that number within 5 tries. If the user successfully guesses the number, he would be redirected to the next level. Else, the game is over. When The user cannot guess that number the should be over but in my case, it is again asking for the same level and there is no output of game over. Here's my code:
from random import randint
level = 1
points = [100,75,50,25,10]
points_scored = 0
play = True
while play:
while level != 0:
end_value = level * 10
secret_number = randint(1,end_value)
for counter in range(0,5):
#gets the guessing number as user input
guess_number = int(input("Guess the secret number between 1 and {} ({} attempts left):".format(end_value,5-counter)))
#check guessing number is correct or higher or lower
if secret_number == guess_number:
print("Your Guess is Correct, You Won the level {} with {} points".format(level,points[counter]))
points_scored = points[counter]
level = level 1
break
elif guess_number < secret_number:
print("Your Guess is lower than secret number")
elif guess_number > secret_number:
print("Your Guess is higher than secret number")
else:#else of for statement
print("Game Over, You Loose the Game, secret number is {} and your total scores are".format(secret_number, points[counter]))
break
else:#else of while statement
print("Congratz, You Won the Game with {} !!!".format(points_scored))
again=str(input("Do you want to play again, type yes or no "))
if again == "no":
play = False
quit()
CodePudding user response:
The logic is almost completely right but there is a big problem in your code. Think of the conditions you have set in the most inner loop:
if secret_number == guess_number:
...
elif guess_number < secret_number:
...
elif guess_number > secret_number:
...
else:
# How can a number be not equal, not bigger, and not smaller than another number???
I think some refactoring could be made to make the code even cleaner and perform correctly:
from random import randint
level = 1
points = [100, 75, 50, 25, 10]
points_scored = 0
# No need to use the play variable, the program exits in the end with "quit" if the user doesn't want to keep playing
# You don't seem to set the level as 0 anywhere so why not just keep playing until the user loses.
while True:
end_value = level * 10
secret_number = randint(1, end_value)
# The first parameter in range is already 0, no need to explicitly set it
for counter in range(5):
# gets the guessing number as user input
guess_number = int(input("Guess the secret number between 1 and {} ({} attempts left):".format(end_value, 5 - counter )))
# check guessing number is correct or higher or lower
if secret_number == guess_number:
print("Your Guess is Correct, You Won the level {} with {} points".format(level, points[counter]))
points_scored = points[counter]
# level = 1 is the same as level = level 1
level = 1
break
elif guess_number < secret_number:
print("Your Guess is lower than secret number")
# This next one could be "else:" because, as stated before,
# if guess_number is not equal nor smaller than secret_number,
# it can only be bigger, but it makes no difference.
elif guess_number > secret_number:
print("Your Guess is higher than secret number")
else:
print("Game Over, You Loose the Game, secret number is {}. You got {} points.".format(secret_number, points_scored))
# No need to cast the input as str, it's already a string.
again = input("Do you want to play again, type yes or no ")
if again == "no":
quit()
else:
level = 1
CodePudding user response:
The reason you are coming back to the same level is because your else statement is not executing. One way to correct this would be to remove the else statement and bring it out of the counter loop. Also add a flag to keep the losing status of the player. Change:
elif guess_number > secret_number:
print("Your Guess is higher than secret number")
else:#else of for statement
print("Game Over, You Loose the Game, secret number is {} and your total scores are".format(secret_number, points[counter]))
break
to:
elif guess_number > secret_number:
print("Your Guess is higher than secret number")
if level_lost:
print("Game Over, You Loose the Game, secret number is {} and your total scores are".format(secret_number, points[counter]))
level = 0
break
Set the losing status to false if the player correctly guesses the number.
while level != 0:
level_lost = True
end_value = level * 10
secret_number = randint(1,end_value)
for counter in range(0,5):
#gets the guessing number as user input
guess_number = int(input("Guess the secret number between 1 and {} ({} attempts left):".format(end_value,5-counter)))
#check guessing number is correct or higher or lower
if secret_number == guess_number:
print("Your Guess is Correct, You Won the level {} with {} points".format(level,points[counter]))
points_scored = points[counter]
level = level 1
level_lost = False
Then finally to restart the game again change
if again == "no":
play = False
to:
if again == "no":
play = False
quit()
else:
level=1
CodePudding user response:
The way you have written the code is correct but it is looping again. I made some modifications to your code using while loop.
from random import randint
level = 1
points = [100,75,50,25,10]
points_scored = 0
play = True
while play:
while level != 0:
end_value = level * 10
secret_number = randint(1,end_value)
counter=0
while counter<5:
#gets the guessing number as user input
guess_number = int(input("Guess the secret number between 1 and {} ({} attempts left):".format(end_value,5-counter)))
#check guessing number is correct or higher or lower
if secret_number == guess_number:
print("Your Guess is Correct, You Won the level {} with {} points".format(level,points[counter]))
points_scored = points[counter]
level = level 1
break
elif guess_number < secret_number:
print("Your Guess is lower than secret number")
elif guess_number > secret_number:
print("Your Guess is higher than secret number")
counter=counter 1
if(counter>4):
print("Game Over, You Loose the Game, secret number is {} and your total scores are{}".format(secret_number, points[counter-1]))
break
break
print("Congratz, You Won the Game with {} !!!".format(points_scored))
again=str(input("Do you want to play again, type yes or no "))
if again == "no":
play = False
quit()
else:
level=1
Here, game over will be printed when the number of guesses are exceeded.