问题
Apologies if a version of this question has been asked before. I looked through but couldn't find something that really addressed my problem.
I started learning python today and have tried to build a simple Rock, Paper, Scissors game.
I have the following code and it works pretty well:
import random
choices = ["rock", "paper", "scissors"]
player_move = input("Enter your move... ").lower()
cpu_move = random.choice(choices)
play = True
result_1 = ("Computer: " + cpu_move)
result_2 = ("You :" + player_move)
player_counter = 0
cpu_counter = 0
while play == True:
print(result_2)
print(result_1)
if cpu_move == "rock" and player_move == "paper" or cpu_move == "paper" and player_move == "scissors" or cpu_move == "scissors" and player_move == "rock":
print("You win!")
player_counter =+ 1
print("Computer Score: " + str(cpu_counter))
print("Your Score: " + str(player_counter))
play = False
elif cpu_move == "rock" and player_move == "scissors" or cpu_move == "paper" and player_move == "rock" or cpu_move == "scissors" and player_move == "paper":
print("You lose!")
cpu_counter += 1
print("Computer Score: " + str(cpu_counter))
print("Your Score: " + str(player_counter))
play = False
else:
print("It's a tie!")
play = True
However, I can't work out how to do the following two things:
1) allow the user to enter a new move if the game is tied. 2) repeat the game regardless of outcome buy keep the score counter going.
Any help would be enormously appreciated!
Thanks, Jason
回答1:
If you moved this line
player_move = input("Enter your move... ").lower()
inside the while loop, you should be able to both
- allow the user to enter a new move if the game is tied, and
- repeat the game regardless of outcome but keep the score counter going.
Of course, you'd also need to move the code assigning cpu_move
, player_move
, result_1
and result_2
inside the while loop as well.
Conceptually, you can think of everything outside of the while loop as setup for the game, and everything inside it as what happens on a single turn.
回答2:
To continue the game for multiple moves, move the statements that process the players' choices inside the while loop.
import random
choices = ["rock", "paper", "scissors"]
play = True
player_counter = 0
cpu_counter = 0
while play == True:
player_move = input("Enter your move... ").lower()
cpu_move = random.choice(choices)
result_1 = ("Computer: " + cpu_move)
result_2 = ("You :" + player_move)
print(result_2)
print(result_1)
if cpu_move == "rock" and player_move == "paper" or cpu_move == "paper" and player_move == "scissors" or cpu_move == "scissors" and player_move == "rock":
print("You win!")
player_counter =+ 1
print("Computer Score: " + str(cpu_counter))
print("Your Score: " + str(player_counter))
play = False
elif cpu_move == "rock" and player_move == "scissors" or cpu_move == "paper" and player_move == "rock" or cpu_move == "scissors" and player_move == "paper":
print("You lose!")
cpu_counter += 1
print("Computer Score: " + str(cpu_counter))
print("Your Score: " + str(player_counter))
play = False
else:
print("It's a tie!")
play = True
来源:https://stackoverflow.com/questions/60103238/repeat-game-python-rock-paper-scissors