如何用python完成评分功能呢_如何让Python评分系统正常工作呢?

博主创建了一个石头-布-剪刀游戏并尝试添加得分跟踪功能,遇到问题。全局变量无法正确保存分数,导致每次游戏重新开始时分数重置。此外,`main()`函数中的选项2总是提示“1不是一个选项”。博主寻求解决方案,希望能理解为何会出现这种情况并修复代码,以实现游戏得分的持久记录。
摘要由CSDN通过智能技术生成

我一直在通过一个在线课程学习,我试图想出一些我可以创造的东西来“测试”自己,所以我想出了一个石头-布-剪刀游戏。它运行得很好,所以我决定尝试添加一种方法来跟踪你与电脑的比分。不是很顺利。在

以下是我所拥有的:from random import randint

ai_score = 0

user_score = 0

def newgame():

print('New Game')

try:

while(1):

ai_guess = str(randint(1,3))

print('\n1) Rock \n2) Paper \n3) Scissors')

user_guess = input("Select An Option: ")

if(user_guess == '1'):

print('\nYou Selected Rock')

elif(user_guess == '2'):

print('\nYou Selected Paper')

elif(user_guess == '3'):

print('\nYou Selected Scissors')

else:

print('%s is not an option' % user_guess)

if(user_guess == ai_guess):

print('Draw - Please Try Again')

elif (user_guess == '1' and ai_guess == '2'):

print("AI Selected Paper")

print("Paper Beats Rock")

print("AI Wins!")

ai_score += 1

break

elif (user_guess == '1' and ai_guess == '3'):

print("AI Selected Scissors")

print("Rock Beats Scissors")

print("You Win!")

user_score += 1

break

elif (user_guess == '2' and ai_guess == '1'):

print("AI Selected Rock")

print("Paper Beats Rock")

print("You Win!")

user_score += 1

break

elif (user_guess == '2' and ai_guess == '3'):

print("AI Selected Scissors")

print("Scissors Beats Paper")

print("AI Wins!")

ai_score += 1

break

elif (user_guess == '3' and ai_guess == '1'):

print("AI Selected Rock")

print("Rock Beats Scissors")

print("AI Wins!")

ai_score += 1

break

elif (user_guess == '3' and ai_guess == '2'):

print("AI Selected Paper")

print("Scissors Beats Paper")

print("You Win!")

user_score += 1

break

else:

pass

break

except keyboardInterrupt:

print("\nKeyboard Interrupt - Exiting...")

exit()

#1 = Rock, 2 = Paper, 3 = Scissors

def main():

while(1):

print("\n1) New Game \n2) View Score \n3) Exit")

try:

option = input("Select An Option: ")

if option == '1':

newgame()

if option == '2':

print("\nScores")

print("Your Score: " + str(user_score))

print("AI Score: " + str(ai_score))

elif option == '3':

print('\nExiting...')

break

else:

print('%s is not an option' % option)

except KeyboardInterrupt:

print("\nKeyboard Interrupt - Exiting...")

exit()

main()

我在某个地方读到,全局变量可以工作,但通常不受欢迎。不知道为什么,但我不能说他们=0,所以不能让它发挥作用。将ai_score和user_score放入newgame()中不起作用,因为它会在每次运行时将其设置为0。任何帮助都将不胜感激。在

作为一个快速的附加说明,第二个

^{pr2}$

in main()似乎总是执行,总是说“1不是一个选项”,我不知道它为什么这样做。我会假设与while循环有关,但我需要这些来保持它的运行,所以解释一下为什么和如何修复会很好。归根结底,我只是来学习更多。在

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值