python 循环计算分数

python counting score in a loop

我制作了一款运行良好的石头剪刀布游戏,但我想为用户和 AI 添加一个分数,计算每个人赢了多少次。我尝试指定一对分数变量,然后每次都给它加 1,但它一直给出 0。我将增量保留在第一个 if 语句中以表明我的意思。我也尝试了 string.count 但它没有用。任何帮助将不胜感激。

import sys
from random import randint
import math
choice1=input("would you like to play a game of rock,paper and scissors: ")
while choice1=="yes":
    choice2=input("please choose rock,paper or scissor: ")
    computer=randint(1,3)
    score=0
    score2=0
    if choice2 in ("rock","paper","scissor"):
        if computer==1:
            print("You have chosen "+ choice2 + " and the computer has chosen rock ")
            if choice2=="rock":
                print("It'/s a draw ")
            elif choice2=="paper":
                print("You win! ")
                score + 1
            elif choice2=="scissor":
                print("You lose :( ")
                score2 + 
            else:
                sys.exit()
        elif computer==2:
            print("You have chosen " + choice2 + " and the computer has chosen paper ")
            if choice2=="rock":
                print("You lost :( ")
            elif choice2=="paper":
                print("It'/s a draw")
            elif choice2=="scissor":
                print("You won! ")
            else:
                sys.exit()
        elif computer==3:
            print("You have chosen " + choice2 + " and the computer has chosen scissor ")
            if choice2=="rock":
                print("You won! ")
            elif choice2=="paper":
                print("you lost :( ")
            elif choice2=="scissor":
                print("It'/s a draw ")
            else:
                sys.exit()
        choice3=input("Would you like to play again? Type yes or no: ")
        if choice3=="no":
            print (score)
            print (score2)
            sys.exit()
    else:
        sys.exit()
else:
    print("GoodBye")
    sys.exit()

您增加了分数,但未能存储该值。尝试

score += 1
...
score2 += 1

另外,在while循环之前将分数设置为0:只在游戏开始时一次。你在每一轮中重置它们。

为避免在循环的每次迭代中将 score 和 score1 的值重置为 0,您应该在 while 循环上方初始化这两个变量。

然后您应该使用

增加值
score += 1

score1 += 1