Reputation: 154
I am creating a program where a user had to guess a random number in a specific range and has 3 tries to do so. After each try, if the guess is correct you tell the user they won and if the guess is wrong you tell them its wrong and how many tries they have got remaining. When the number of tries exceed 3 without a correct guess, you tell the user they are out of tries and tell them they lost. When the program finishes after a correct or 3 wrong tries and the game is over, you ask if the user wants to play again. If they say "yes", you star the game again and if they say "no", you end the game. This is the code I have come up with:
import random
x =round(random.random()*5)
guess = False
tries = 0
while guess != True:
if tries<=3:
inp = input("Guess the number: ")
if tries<3:
if inp == x:
guess = True
print "Correct! You won"
else:
print "Wrong guess! Try again."
tries = tries + 1
print "Your remaining tries are",(3-tries)
if tries>2:
if inp == x:
guess = True
print "Correct! You won"
else:
print "Wrong guess! You are out of tries"
print "Game Over!"
again = raw_input("Do you want to play again? ")
if again == "no":
print "Okay thanks for playing!"
elif again =="yes":
print "Great!"
Now when you guess the correct number before 3 tries, the program seems to be working fine, but even when the number of wrong tries exceeds 3, the program keeps asking for more guesses but I want it to stop the program right there. The second thing I don't get is how do I start the whole game again when the user wants to play again? How can I modify my code to fix these errors?
Upvotes: 1
Views: 286
Reputation: 476
I guess this is it. Tested this out. Working Perfect (Y)
import random
x =round(random.random()*5)
guess = False
tries = 0
while guess != True:
if tries<=3:
inp = input("Guess the number: ")
if tries<3:
if inp == x:
guess = True
print "Correct! You won"
else:
print "Wrong guess! Try again."
tries = tries + 1
print "Your remaining tries are",(3-tries)
if tries>2:
if inp == x:
guess = True
print "Correct! You won"
else:
print "Wrong guess! You are out of tries"
print "Game Over!"
again = raw_input("Do you want to play again? ")
if again == "no":
guess = True
print "Okay thanks for playing!"
elif again =="yes":
tries = 0
print "Great!"
Hope this helps.
Here's more compact code you can try. Also it is solving your "Wrong Guess. Try Again." issue on last guess.
import random
x =round(random.random()*5)
guess = False
tries = 0
print x
while guess != True:
while tries<3:
inp = input("Guess the number: ")
if inp == x:
guess = True
print "Correct! You won"
break
else:
tries = tries + 1
if tries == 3:
break
print "Wrong guess! Try again."
print "Your remaining tries are", (3 - tries)
if not guess:
print "Wrong guess! You are out of tries"
print "Game Over!"
again = raw_input("Do you want to play again? ")
if again == "no":
guess = True
print "Okay thanks for playing!"
elif again == "yes":
tries = 0
if guess:
guess = False
x = round(random.random() * 5)
print "Great!"
Upvotes: 3
Reputation: 195
this is a slightly different version to your problem:
import random
def play():
x = round(random.random()*5)
guess = False
tries = 0
while guess is False and tries < 3:
inp = raw_input("Guess the number: ")
if int(inp) == x:
guess = True
print "Correct! You won"
else:
print "Wrong guess! Try again."
tries = tries + 1
print "Your remaining tries are",(3-tries)
if tries == 3:
print("Game Over!")
play()
again = "maybe"
while again != "no":
again = raw_input("Do you want to play again? yes OR no? \n")
if again == "yes":
print "Great!"
play()
print "Okay thanks for playing!"
Upvotes: 2
Reputation: 518
You need to make guess equal to True
in your second else
or else it will never satisfy stop condition for while loop. Do you understand?
import random
x =round(random.random()*5)
guess = False
tries = 0
while guess != True:
if tries<=3:
inp = input("Guess the number: ")
if tries<3:
if inp == x:
guess = True
print "Correct! You won"
elif tries+1 != 3:
print "Wrong guess! Try again."
print "Your remaining tries are",(3-(tries+1))
tries = tries + 1
if tries>2:
if inp == x:
guess = True
print "Correct! You won"
else:
print "Wrong guess! You are out of tries"
print "Game Over!"
guess= True
again = raw_input("Do you want to play again? ")
if again == "no":
print "Okay thanks for playing!"
elif again =="yes":
print "Great!"
guess = False
tries = 0
EDIT: I think this does the trick using your code.
Upvotes: 1
Reputation: 3842
The simplest way to stop a loop when some condition becomes true is to use break
. So when the player guesses correctly, the code would look like
if inp == x:
guess = True
print "Correct! You won"
break
Additionally, you should break the loop when you run out of guesses, otherwise it will keep running
else:
print "Wrong guess! You are out of tries"
print "Game Over!"
break
To let the player play again, you can have a variable playing
and use it in a while loop that wraps the whole game, and when they say they don't want to play anymore, make it False
so that the outer loop ends.
playing = True
while playing:
x = round(random.random()*5)
guess = False
tries = 0
while guess != True:
...
again = raw_input("Do you want to play again? ")
if again == "no":
print "Okay thanks for playing!"
playing = False
elif again =="yes":
print "Great!"
As you noted in comments on another answer, when the player gets their third guess wrong, the game tells them they have 0 guesses left and to try again, and then tells them they are out of tries and that the game is over. Since you want it to only tell them they are out of tries and that the game is over, you can change the main block of code to:
while guess != True:
inp = input("Guess the number: ")
if inp == x:
guess = True
print "Correct! You won"
else:
tries = tries + 1
if tries == 3:
print "Wrong guess! You are out of tries"
print "Game Over!"
break
else:
print "Wrong guess! Try again."
print "Your remaining tries are",(3-tries)
Upvotes: 3
Reputation: 770
You need to move where you set guess = True
import random
x =round(random.random()*5)
guess = False
tries = 0
while guess != True:
if tries<=3:
inp = input("Guess the number: ")
if tries<3:
if inp == x:
guess = True
print "Correct! You won"
else:
print "Wrong guess! Try again."
tries = tries + 1
print "Your remaining tries are",(3-tries)
if tries>2:
guess = True
if inp == x:
print "Correct! You won"
else:
print "Wrong guess! You are out of tries"
print "Game Over!"
again = raw_input("Do you want to play again? ")
if again == "no":
print "Okay thanks for playing!"
elif again =="yes":
print "Great!"
Before you were only ending on the condition where the last guess was correct. You should be ending whether the guess was correct or not.
Upvotes: 1
Reputation: 1371
You need to update tries. Additionally you need to break out of your loop even if guess != True
import random
x =round(random.random()*5)
guess = False
tries = 0
while guess != True:
if tries<=3:
inp = input("Guess the number: ")
if tries<3:
if inp == x:
guess = True
print "Correct! You won"
else:
print "Wrong guess! Try again."
tries = tries + 1
print "Your remaining tries are",(3-tries)
if tries>2:
if inp == x:
guess = True
print "Correct! You won"
else:
print "Wrong guess! You are out of tries"
print "Game Over!"
break
tries += 1
again = raw_input("Do you want to play again? ")
if again == "no":
print "Okay thanks for playing!"
elif again =="yes":
print "Great!"
Upvotes: 0