我正在编写的程序是一个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))
在以下情况下使用and
代替:or
while
while user_score != 5 and cpu_score != 5:
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句