python在循环中计算分数
我制作了一个石头剪刀游戏,效果很好,但我想为用户和人工智能添加一个分数,计算每个人赢了多少次。我试着用一对分数变量,然后每次加1,但它总是给0。我在第一个if语句中保留了增量,以显示我的意思。我还尝试了string.count,但没有成功。任何帮助都将不胜感激python在循环中计算分数,python,counting,Python,Counting,我制作了一个石头剪刀游戏,效果很好,但我想为用户和人工智能添加一个分数,计算每个人赢了多少次。我试着用一对分数变量,然后每次加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 scisso
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
及
当时,您可以在
顶部将分数
重置为0
。将其移动到while语句上方。刚刚尝试,它仍然输出0
score1 += 1