我一直在通過一個在線課程學習,我試圖想出我可以創建的東西來「測試」自己,因爲它是這樣的,所以我想出了一個岩石剪刀遊戲。它運行良好,所以我決定嘗試添加一種跟蹤你的分數與計算機的方式。沒有那麼好。如何讓我的Python評分系統工作?
這是我有:
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。任何幫助將非常感激。
作爲一個快速額外的說明,第二
else:
print('%s is not an option' % option)
在主
()似乎總是執行並總是說「1是不是一種選擇」,我不知道爲什麼會做到這一點。我會假設一些與while循環有關的事情,但我需要這些來保持它運行,所以解釋爲什麼以及如何修復會很好。在一天結束時,我只是在這裏瞭解更多。
使用'key:value'對創建一個詞典以及它們擊敗了什麼,它會使得比較更容易,並且您不必分配數字 – Andrew
如果您希望可以創建一個班級並將分數設置爲一個類變量,其中方法(newgame)會更新該類變量。主要你可以問這個對象的類變量的分數是多少。這可以接受嗎? – MooingRawr
您使用的是哪個版本的python? –