Reputation: 47
I'm trying to make an old rock, paper, scissors game I made better by asking the user if they would like to replay or not..
This is the code I have been working on:
# ROCK, PAPER, SCISSORS !!!
# BY: NICK GRIMES
from random import randint
replay = True
while replay == True:
replay = False
userChoice = raw_input("> Choose rock, paper, or scissors: ")
computerChoice = randint(1, 101)
if computerChoice <= 34:
computerChoice = "rock"
elif computerChoice <= 67:
computerChoice = "paper"
else:
computerChoice = "scissors"
print "> The computer chooses: " + str(computerChoice)
if userChoice == computerChoice:
print "> It's a tie! Thank you for playing!"
askUserPlayAgain = raw_input("> Would you like to play again? Enter [yes or no]: ")
if askUserPlayAgain.lower()[0] == "y":
replay == True
elif askUserPlayAgain.lower()[0] == "n":
break
# ---
elif userChoice == "rock":
if computerChoice == "scissors":
print "> Rock crushes scissors!"
print "> You win!"
else:
print "> Paper covers rock!"
print "> Computer wins!"
# ---
elif userChoice == "paper":
if computerChoice == "rock":
print "> Paper coveres rock!"
print "> You win!"
else:
print "> Scissors cuts paper!"
print "> Computer wins!"
# ---
elif userChoice == "scissors":
if computerChoice == "rock":
print "> Rock crushes scissors"
print "> Computer wins!"
else:
print "> Scissors cuts paper"
print "> You win!"
# ---
else:
print "Please choose either rock, paper, or scissors only!"
Now, the actual game has always worked, but with trying to implement this new idea, I actually deleted the function that compared the two choices, and just left them to the if/else's...
The main problem is this: when a user types 'yes', it should restart the game, but it does absolutely nothing for some reason. If anyone see's anything that could be causing this, please let me know. Thank you!
Upvotes: 0
Views: 104
Reputation: 3192
This doesn't make much sense to me:
replay = True
while replay == True:
replay = False
I think you'd be better off using a while True:
loop. Use the continue
keyword if the user selects to restart the game and the replay variable is not required.
from random import randint
while True:
userChoice = raw_input("> Choose rock, paper, or scissors: ")
computerChoice = randint(1, 101)
if computerChoice <= 34:
computerChoice = "rock"
elif computerChoice <= 67:
computerChoice = "paper"
else:
computerChoice = "scissors"
print("> The computer chooses: " + str(computerChoice))
if userChoice == computerChoice:
print("> It's a tie! Thank you for playing!")
askUserPlayAgain = raw_input("> Would you like to play again? Enter [yes or no]: ")
if askUserPlayAgain.lower()[0] == "y":
continue
elif askUserPlayAgain.lower()[0] == "n":
break
Upvotes: 1
Reputation: 1097
Just changed
while replay == True:
to
while True:
and it just work fine for me.
Upvotes: 1