[英]While loop is not breaking when condition is met, What am I doing wrong?
我正在编写的程序是一个 Python 3.5 剪刀石头布游戏,它会玩剪刀石头布游戏,直到 cpu 或玩家得分为 5。我能想到的最好方法是在 while 循环中执行此操作,但犯了一些错误我的游戏无限继续,当分数变为 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))
在while
条件下使用and
代替or
:
while user_score != 5 and cpu_score != 5:
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.