我一直在通过一个在线课程学习,我试图想出一些我可以创造的东西来“测试”自己,所以我想出了一个石头-布-剪刀游戏。它运行得很好,所以我决定尝试添加一种方法来跟踪你与电脑的比分。不是很顺利。在
以下是我所拥有的: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循环有关,但我需要这些来保持它的运行,所以解释一下为什么和如何修复会很好。归根结底,我只是来学习更多。在