我正在编写的程序是一个Python 3.5石头剪刀游戏,它玩石头剪刀,直到cpu或玩家得到5分 . 我能想到的最好的方法是在一个循环中,但由于一些错误当我的分数变为5时,游戏继续无限地循环并且循环不会中断 . 我会在这里粘贴代码(我希望我的评论很有帮助 . )在此先感谢您的帮助,我知道这非常简单和愚蠢!
#import the "random" library
import random
#Welcome the user and explain the program
print("Welcome to the Rock Paper Scissor game! You will play until you or the computer gets 5 wins!")
#Set constants for rock paper and scissors for the computer's choice
ROCK = 1
PAPER = 2
SCISSOR = 3
#Define the scores for the user and computer and start them as 0
user_score = 0
cpu_score = 0
#Start the loop that runs until the user or computer reaches the score of 5
while user_score != 5 or cpu_score != 5:
#Gets the choice of the user and stores it in a variable
choice = input("Please enter your choice- 'r'ock, 'p'aper, or 's'cissors? \n")
#Prevents the loop from progressing until the user picks a valid command
while choice != "r" and choice != "p" and choice != "s":
choice = input("Invalid command: Please enter your choice- 'r'ock, 'p'aper, or 's'cissors? \n")
#get's a random pick between 1 and 3 for the cpu's choice
cpu_pick = random.randint(ROCK,SCISSOR)
#Prints the pick of the user prior to determining the winner so this does not have to be included in every if statement
if choice == "r":
print("You pick rock!")
elif choice == "s":
print("You pick scissors!")
elif choice == "p":
print("You pick paper!")
#Prints the pick of the cpu prior to determining the winner so this does not have to be included in every if statement
if cpu_pick == ROCK:
print("The cpu picks a rock!\n")
elif cpu_pick == SCISSOR:
print("The cpu picks scissors!\n")
elif cpu_pick == PAPER:
print("The cpu picks paper!\n")
#Accounts for all cases when the cpu pick is rock and adds to the scores when somebody wins
if cpu_pick == ROCK and choice == "r":
print("Tie! New round!")
elif cpu_pick == ROCK and choice == "s":
print("CPU wins this round!")
cpu_score += 1
elif cpu_pick == ROCK and choice == "p":
print("You win this round!")
user_score += 1
#Accounts for all cases when the cpu pick is Scissors and adds to the scores when somebody wins
if cpu_pick == SCISSOR and choice == "s":
print("Tie! New round!")
elif cpu_pick == SCISSOR and choice == "p":
print("CPU wins this round!")
cpu_score += 1
elif cpu_pick == SCISSOR and choice == "r":
print("You win this round!")
user_score += 1
# Accounts for all cases when the cpu pick is Paper and adds to the scores when somebody wins
if cpu_pick == PAPER and choice == "p":
print("Tie! New round!")
elif cpu_pick == PAPER and choice == "r":
print("CPU wins this round!")
cpu_score += 1
elif cpu_pick == PAPER and choice == "s":
print("You win this round!")
user_score += 1
#Prints the score after each round
print("Score: \nComputer: %d\nYou: %d" % (cpu_score, user_score))
#when the loop is broken check who won then print the final score and winner
if user_score == 5:
print("You win by a score of %d to %d!" % (user_score, cpu_score))
elif user_score == 5:
print("The CPU won bu a score of %d to %d!" % (cpu_score, user_score))
1 回答
在
while
条件中使用and
而不是or
: