循环中的python计数得分

时间:2017-10-30 22:30:29

标签: python counting

我做了一个摇滚,剪刀游戏,工作正常,但我想为用户和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()

2 个答案:

答案 0 :(得分:1)

您增加分数,但无法存储该值。尝试

score += 1
...
score2 += 1

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

答案 1 :(得分:0)

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

然后你应该使用

增加值
score += 1

score1 += 1