2016-09-08 19 views
1

我一直在通過一個在線課程學習,我試圖想出我可以創建的東西來「測試」自己,因爲它是這樣的,所以我想出了一個岩石剪刀遊戲。它運行良好,所以我決定嘗試添加一種跟蹤你的分數與計算機的方式。沒有那麼好。如何讓我的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循環有關的事情,但我需要這些來保持它運行,所以解釋爲什麼以及如何修復會很好。在一天結束時,我只是在這裏瞭解更多。

+0

使用'key:value'對創建一個詞典以及它們擊敗了什麼,它會使得比較更容易,並且您不必分配數字 – Andrew

+0

如果您希望可以創建一個班級並將分數設置爲一個類變量,其中方法(newgame)會更新該類變量。主要你可以問這個對象的類變量的分數是多少。這可以接受嗎? – MooingRawr

+0

您使用的是哪個版本的python? –

回答

0

至少有一個問題是這樣的:你的main有if ... if ... elif ... else。第二個如果可能需要成爲elif。提示:當出現流程控制問題時,請在每個控制分支中放置打印語句,打印出控制變量以及可能相關的其他所有內容。這告訴你哪個分支正在被採用 - 在這種情況下,哪個分支,複數。

你不確切地說保持得分的問題是什麼,但我想它是沿賦值前引用的變量的一個例外。如果是這樣,你應該把「全局ai_score」放在函數的頂部。發生了什麼事是,Python可以但不喜歡識別函數內部正在使用的函數外部的變量。你必須推一點。考慮:

>>> bleem = 0 
>>> def incrbleem(): 
... bleem += 1 
... 
>>> incrbleem() 
Traceback (most recent call last): 
    File "<stdin>", line 1, in <module> 
    File "<stdin>", line 2, in incrbleem 
UnboundLocalError: local variable 'bleem' referenced before assignment 
>>> def incrbleem(): 
... global bleem 
... bleem += 1 
... 
>>> bleem 
0 
>>> incrbleem() 
>>> bleem 
1 

順便說一句,你的代碼根本不差,對於一個新手來說。我看過很多,更糟!對於它的價值,我不認爲全局變量對於像這樣的小型拋棄式程序是不利的。一旦你有兩個程序員,或兩個線程,或兩個月之間的程序工作會話,全局變量肯定會導致問題。

+0

我明白了。謝謝你的幫助。當我第一次寫它的時候,我已經確定了和elif完全相同的問題,但是我顯然意外地解開了它,並且沒有想到再次看到那裏。我會嘗試添加你的代碼,因爲編程對我來說更像是一種愛好,因爲不幸的是,我的大學不提供計算機科學: – bobvader2001

2
from random import randint 

class newgame(): 

    ai_score = 0 
    user_score = 0 

    def __init__(self): 
     self.ai_score = 0 
     self.user_score = 0 

    def playgame(self): 
     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!") 
        self.ai_score += 1 
        break 
       elif (user_guess == '1' and ai_guess == '3'): 
        print("AI Selected Scissors") 
        print("Rock Beats Scissors") 
        print("You Win!") 
        self.user_score += 1 
        break 
       elif (user_guess == '2' and ai_guess == '1'): 
        print("AI Selected Rock") 
        print("Paper Beats Rock") 
        print("You Win!") 
        self.user_score += 1 
        break 
       elif (user_guess == '2' and ai_guess == '3'): 
        print("AI Selected Scissors") 
        print("Scissors Beats Paper") 
        print("AI Wins!") 
        self.ai_score += 1 
        break 
       elif (user_guess == '3' and ai_guess == '1'): 
        print("AI Selected Rock") 
        print("Rock Beats Scissors") 
        print("AI Wins!") 
        self.ai_score += 1 
        break 
       elif (user_guess == '3' and ai_guess == '2'): 
        print("AI Selected Paper") 
        print("Scissors Beats Paper") 
        print("You Win!") 
        self.user_score += 1 
        break 
       else: 
        pass 
        break 

     except KeyboardInterrupt: 
      print("\nKeyboard Interrupt - Exiting...") 
      exit() 

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


def main(): 
    game_object = newgame() 
    while(1): 
     print("\n1) New Game \n2) View Score \n3) Exit") 
     try: 
      option = input("Select An Option: ") 

      if option == '1': 
       game_object.playgame() 
      elif option == '2': 
       print("\nScores") 
       print("Your Score: " + str(game_object.user_score)) 
       print("AI Score: " + str(game_object.ai_score)) 
      elif option == '3': 
       print('\nExiting...') 
       break 
      else: 
       print('%s is not an option' % option) 
     except KeyboardInterrupt: 
      print("\nKeyboard Interrupt - Exiting...") 
      exit() 


main() 

上課很棒。 __init__是這個類的構造函數。它基本上使對象立即爲類,並將變量設置爲你想要的。 game_object = newgame()生成類對象並將其存儲到game_object中。要獲得game_object的類變量,我們使用game_object.ai_score。既然你創建了一個類對象,它的類變量仍然在你創建的對象的範圍內,儘管它可能在你的函數之外。一般來說,如果我需要在函數外部使用一個變量,並且試圖使用Global,我會改爲創建一個類。有些情況下你不想要這個,但是我個人並沒有遇到過這種情況。你也可能想看看評論中關於使用字典作爲選項的評論。還有其他問題嗎?

編輯:

要回答你的新問題有關print('%s is not an option' % option)始終處於運行狀態,是因爲在你的代碼中有if option == '1':然後if option == '2':您想要的選項2是ELIF。我在我的代碼中修復了它。如果語句在塊中。既然你開始了一個新的if,那麼else不會先檢查是否有效的選項。這在某種意義上超出了範圍。所以,因爲你的代碼基本上是說選項等於1?它等於2還是3還是其他?注意這些是兩個不同的問題?

+0

我只是在看完整的超級基礎課程從頭開始0 python的知識,它甚至沒有提到類xD我甚至不知道它們是在Python中的東西。還有什麼與雙下劃線任何一側的init?我已經看過幾次, m不知道這意味着什麼 – bobvader2001

+0

http://stackoverflow.com/questions/8689964/why-do-some-functions-have-underscores-before-and-after-the-function-name TL; DR:尾部下劃線是避免關鍵字''class_'是可用的。領先的下劃線得到更加棘手,'__hello'是「私人」,但不是真的。它不是像java或c#私有變量。它也是一樣的,但你仍然可以訪問它們我知道這是conf使用不用擔心,有更多的人解釋它比我更好。 – MooingRawr

+0

'__init__'被設置爲一種「魔法方法」,你不會惹他們。 http://stackoverflow.com/questions/2657627/why-does-python-use-magic-methods – MooingRawr

1

看來變量option不是'1',對吧?那是因爲函數input不返回一個字符串,而是一個integer。你可以通過在這個程序中添加一點點痕跡來看到這一點。

print (option, type (option)) 

在設置了變量選項的行之後。

這會告訴你變量選項是什麼類型,在這種情況下它是一個整數。因此,首先,你需要對整數進行比較,以取代比較反對的字符串(如if option == '1':,那就是:if option == 1:

至於第二個問題:變量聲明或指定函數內部存在該函數的範圍僅。如果你需要在一個具有外部作用域的函數內部使用一個變量,它應該在函數內重新聲明爲global(即使它們被「皺起眉頭」 - 並且有很好的理由)在def newgame():您需要再次聲明您的全局變量:global ai_score, user_score。您還可以使用類來熟悉面向對象的編程並編寫更好的代碼。此程序中還存在其他錯誤,但我相信您會發現。