当满足条件时循环没有中断,我在做什么错呢?

布伦丹·谢尔曼

我正在编写的程序是一个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))
acw1668

在以下情况下使用and代替orwhile

while user_score != 5 and cpu_score != 5:

本文收集自互联网,转载请注明来源。

如有侵权,请联系[email protected] 删除。

编辑于
0

我来说两句

0条评论
登录后参与评论

相关文章

来自分类Dev

Elasticsearch没有存储字段,我在做什么错?

来自分类Dev

没有CSS被识别。...我在做什么错?

来自分类Dev

Elasticsearch没有存储字段,我在做什么错?

来自分类Dev

查询中的GROUP BY-我在做什么错呢?

来自分类Dev

CSS3过渡中断,我在做什么错?

来自分类Dev

CSS3过渡中断,我在做什么错?

来自分类Dev

对于循环未按预期工作,我在做什么错?

来自分类Dev

在带日期的while循环中我在做什么错?

来自分类Dev

对于循环未按预期工作,我在做什么错?

来自分类Dev

我的缩略图没有分成多列,我在做什么错?

来自分类Dev

使用python登录网站时,我尝试了很多示例,但是我在做什么错呢?

来自分类Dev

Python中的PIL抱怨PixelAccess没有'size'属性,我在做什么错?

来自分类Dev

文件没有被打印出来,我在做什么错?

来自分类Dev

切换场景时我在做什么错?

来自分类Dev

建立创始交易时出错-我在做什么错?

来自分类Dev

我试图用Java中的“ 0”替换Evens,我在做什么错呢?

来自分类Dev

使用Ajax时,我仍然无法使回调工作。它似乎没有在等待回调,我在做什么错?

来自分类Dev

Gruntjs目标我在做什么错?

来自分类Dev

重量我在做什么错?

来自分类Dev

SQL Pivot(我在做什么错)

来自分类Dev

.htaccess我在做什么错?

来自分类Dev

Firestore-我在做什么错?

来自分类Dev

Perlin的声音-我在做什么错?

来自分类Dev

Gruntjs目标我在做什么错?

来自分类Dev

QWinTaskbarProgress我在做什么错?

来自分类Dev

TFS权限-我在做什么错?

来自分类Dev

Sed我在做什么错

来自分类Dev

grep我在做什么错?

来自分类Dev

打印余额,我在做什么错?

Related 相关文章

热门标签

归档