2015-04-22 49 views
1

這是我當前的代碼,當我運行它時,出現一些錯誤......我不確定它們是什麼。我的錯誤在哪裏? (54行代碼)

Traceback (most recent call last):

File "D:/.Actual W4/Lab3Problem1.py", line 54, in main()

File "D:/.Actual W4/Lab3Problem1.py", line 8, in main nestediffunction()

File "D:/.Actual W4/Lab3Problem1.py", line 27, in nestediffunction if (booksread == 0):

NameError: name 'booksread' is not defined

def main(): 
    print("Welcome to Ryan's book club!") 
    booksread = int(input('Please enter the amount of books you have read so far: ')) 
# if (booksread < 0): 
#  print("That's impossible! Please re-enter the amount of books you have read: ") 
#  booksread1 = int(input('Please enter the amount of books you have read so far: '))   
    #simpleiffunction() 
    nestediffunction() 
    #eliffunction() 




def simpleiffunction(): 
    if (booksread == 0): 
     print('You have 0 points!') 
    if (booksread == 1): 
     print('You have 5 points!') 
    if (booksread == 2): 
     print('You have 15 points!') 
    if (booksread == 3): 
     print('You have 30 points!') 
    if (booksread >= 4): 
     print('You have 60 points!') 

def nestediffunction(): 
    if (booksread == 0): 
     print('You have 0 points!') 
    else: 
     if (booksread == 1): 
      print('You have 5 points!') 
     else: 
      if (booksread == 2): 
       print('You have 15 points!') 
      else: 
       if (booksread == 3): 
        print('You have 30 points!') 
       else: 
        if (booksread >= 4): 
         print('You have 60 points!') 

def eliffunction(): 
    if (booksread == 0): 
     print('You have 0 points!') 
    elif (booksread == 1): 
     print('You have 5 points!') 
    elif (booksread == 2): 
     print('You have 15 points!') 
    elif (booksread == 3): 
     print('You have 30 points!') 
    elif (booksread >= 4): 
     print('You have 60 points!') 

main() 

回答

2

您必須傳入主區域中的booksread變量。這允許該功能知道用戶輸入的書籍的編號。該函數使用numbooksread變量來存儲傳遞的變量,以便它可以在函數中使用。

nestediffunction(booksread) 

def nestediffunction(numbooksread): 
if (numbooksread == 0): 
    print('You have 0 points!') 
else: 
    if (numbooksread == 1): 
     print('You have 5 points!') 
    else: 
     if (numbooksread == 2): 
      print('You have 15 points!') 
     else: 
      if (booksread == 3): 
       print('You have 30 points!') 
      else: 
       if (numbooksread >= 4): 
        print('You have 60 points!') 
+0

謝謝,這使它工作。 – Ryan

5

可變booksread是不是在任何它使用nestediffunctioneliffunctionsimpleiffunction功能的範圍。修改這兩個函數,以便您可以接受booksread變量作爲參數,並可以相應地將它從main傳遞。

def simpleiffunction(booksread): 
    .... 

def nestediffunction(booksread): 
    .... 

def eliffunction(booksread): 
    .... 
+0

我添加它,所以它看起來像'高清simpleiffunction(booksread):與其他'等等,但我得到了同樣的錯誤。 '回溯(最近通話最後一個): 文件 「d:/實際W4/Lab3Problem1.py」,第54行,在 的main() 文件「D./.Actual W4 /Lab3Problem1.py」,8號線,在主 nestediffunction() 類型錯誤:nestediffunction()失蹤1個人需要的位置參數:「booksread'' – Ryan

+0

@Ryan:你必須改變的所有功能的簽名。此外,你的函數調用必須符合簽名,即你需要將'booksread'作爲參數傳遞給所有改變的函數 – Abhijit

+0

你可以把它弄瞎了嗎?我無法理解。 – Ryan

0

你可以按照上面的答案,這是完全正確的,你可以在你的代碼結構略有改變這樣的:

def simpleiffunction(): 
    if (booksread == 0): 
     print('You have 0 points!') 
    if (booksread == 1): 
     print('You have 5 points!') 
    if (booksread == 2): 
     print('You have 15 points!') 
    if (booksread == 3): 
     print('You have 30 points!') 
    if (booksread >= 4): 
     print('You have 60 points!') 

def nestediffunction(): 
    if (booksread == 0): 
     print('You have 0 points!') 
    else: 
     if (booksread == 1): 
      print('You have 5 points!') 
     else: 
      if (booksread == 2): 
       print('You have 15 points!') 
      else: 
       if (booksread == 3): 
        print('You have 30 points!') 
       else: 
        if (booksread >= 4): 
         print('You have 60 points!') 

def eliffunction(): 
    if (booksread == 0): 
     print('You have 0 points!') 
    elif (booksread == 1): 
     print('You have 5 points!') 
    elif (booksread == 2): 
     print('You have 15 points!') 
    elif (booksread == 3): 
     print('You have 30 points!') 
    elif (booksread >= 4): 
     print('You have 60 points!') 

if __name__=="__main__": 
    print("Welcome to Ryan's book club!") 
    booksread = int(input('Please enter the amount of books you have read so far: ')) 
# if (booksread < 0): 
#  print("That's impossible! Please re-enter the amount of books you have read: ") 
#  booksread1 = int(input('Please enter the amount of books you have read so far: '))   
    #simpleiffunction() 
    nestediffunction() 
    #eliffunction() 
1

我做了一些調整你的代碼:

class Dummy(): 
    def __init__(self): 
     self.main() 

    def main(self): 
     print("Welcome to Ryan's book club!") 
     booksread = int(input('Please enter the amount of books you have read so far: ')) 
    # if (booksread < 0): 
    #  print("That's impossible! Please re-enter the amount of books you have read: ") 
    #  booksread1 = int(input('Please enter the amount of books you have read so far: '))   
     #simpleiffunction() 
     self.nestediffunction(booksread) 
     #eliffunction() 

    def simpleiffunction(self): 
     if (booksread == 0): 
      print('You have 0 points!') 
     if (booksread == 1): 
      print('You have 5 points!') 
     if (booksread == 2): 
      print('You have 15 points!') 
     if (booksread == 3): 
      print('You have 30 points!') 
     if (booksread >= 4): 
      print('You have 60 points!') 

    def nestediffunction(self, booksread): 
     self.booksread = booksread 
     if (booksread == 0): 
      print('You have 0 points!') 
     else: 
      if (booksread == 1): 
       print('You have 5 points!') 
      else: 
       if (booksread == 2): 
        print('You have 15 points!') 
       else: 
        if (booksread == 3): 
         print('You have 30 points!') 
        else: 
         if (booksread >= 4): 
          print('You have 60 points!') 

    def eliffunction(self): 
     if (booksread == 0): 
      print('You have 0 points!') 
     elif (booksread == 1): 
      print('You have 5 points!') 
     elif (booksread == 2): 
      print('You have 15 points!') 
     elif (booksread == 3): 
      print('You have 30 points!') 
     elif (booksread >= 4): 
      print('You have 60 points!') 

Dummy() 

1)如果你有一個定義類,你可以使用__init__,那麼當你運行這個類的時候,這個定義總是會被執行。
2)定義總是需要一個'自我'。
3)booksread在main()中定義,所以其他定義不知道它的存在。所以,如果你把它放在nestediffunction()中,那麼它也知道它是什麼樣的,它屬於什麼值。

編輯:
如果你不介意,我重寫了你的excersice。
我讓它更簡單一點,它會告訴你更多清晰的作品。

class Dummy(): 
    def __init__(self): 
     self.askUser() 

    def askUser(self): 
     nBooks = int(input('Number of books read: ')) 
     self.numberPoints(nBooks) 

    def numberPoints(self, nBooks): 
     self.nBooks = nBooks 
     if (nBooks == 0): 
      print('You have 0 points!') 
     elif (nBooks == 1): 
      print('You have 5 points!') 
     elif (nBooks == 2): 
      print('You have 15 points!') 
     elif (nBooks == 3): 
      print('You have 30 points!') 
     else: 
      print('You have 60 points!') 

Dummy() 
1

函數中的變量在默認情況下不是全局的,所以它對其他函數不存在。你需要把它作爲一個屬性:

def main(): 
    print("Welcome to Ryan's book club!") 
    booksread = int(input('Please enter the amount of books you have read so far: ')) 
# if (booksread < 0): 
#  print("That's impossible! Please re-enter the amount of books you have read: ") 
#  booksread1 = int(input('Please enter the amount of books you have read so far: '))   
    #simpleiffunction() 
    nestediffunction(booksread) 
    #eliffunction() 

def nestediffunction(booksread): 
    if booksread == 0: # Also, you don't need parenthesis here 
     print('You have 0 points!') 

等等