2017-10-08 58 views
0

我是一名Python初學者,我試圖編寫一個基本上是使用函數的「算命先生」的程序。我在調用函數get_today()時遇到了一個問題,該函數被編寫爲在每月的某天爲用戶輸入一個輸入,並將其作爲整數返回。嘗試調用函數時發生錯誤

然而,當我呼籲功能提示我與雲的錯誤:

TypeError: get_today() missing 1 required positional argument: 'd' 

我試着打了一下週圍,不能弄清楚這是什麼意思。這裏的主要功能是:

def main(): 

    print("Welcome​ ​to​ ​Madame​ ​Maxine's​ ​Fortune​ ​Palace. Here,​ ​we​ ​gaze​ ​deeply into​ ​your​ ​soul​ ​and​ ​find​ ​the secrets​ ​that​ ​only​ ​destiny​ ​has​ ​heretofore​ ​known!") 
    print("") 
    print("The​ ​power​ ​of​ ​my​ ​inner​ ​eye​ ​clouds​ ​my​ ​ability​ ​to​ ​keep track​ ​of mundane​ ​things​ ​like​ ​the​ ​date.") 
    d = get_today() 
    print("Numerology​ ​is​ ​vitally​ ​important​ ​to​ ​fortune​ ​telling.") 
    b = get_birthday() 

    if(d >=1 and d <= 9): 
     print("One more question before we begin.") 
     a = likes_spicy_food() 
     print("I will now read your lifeline") 
     read_lifeline(d,b,a) 
    if(d >= 10 and d <= 19): 
     print("I will now read your heartline.") 
     read_heartline(d,b) 
    if(d >= 20 and d <= 29): 
     print("I need one last piece of information.") 
     m = get_birthmonth() 
     read_headline(b,m) 

    if(d == 30 or d == 31): 
     print("Today is a bad day for fortune telling.") 

     print("These insights into your future are not to be enjoyed or dreaded, they simply come to pass.") 
     print("Good day.") 

main() 

這個問題很可能會重複,當第二功能get_birthday()被調用,詢問他們出生一個月的日子用戶。

這裏是get_today的代碼片段():

def get_today(): 

     x = int(input("Tell​ ​me,​ ​what​ ​day​ ​of​ ​the​ ​month​ ​is​ ​it​ ​today:​")) 

     return x 

幫助將大規模感激!

+1

一方面,功能'get_today'需要一個參數,但是當你所有它'main',你不給它任何參數。看起來你實際上並不需要'get_today'的參數,所以在'def get_today(d):' – jss367

+0

中刪除'd'那麼當你調用'get_today'時,你沒有向它傳遞參數一開始... – toonarmycaptain

+0

刪除d提示我一個新的錯誤'TypeError:get_today()缺少1所需的位置參數:'d'' – dezz

回答

1

當我按原樣運行此代碼時,它不會給我您的錯誤。但是,當我使用d = get_today()作爲d = get_today(d)main下運行此代碼時,出現您收到的錯誤。

當你調用一個函數時,圓括號之間的東西是傳遞給函數的東西。由於您還沒有指定d,因此您無法將其傳入。另外,您的函數不需要傳入變量,因爲它全部是用戶輸入。

試試這個:

def main(): 
    #code 
    d = get_today() 
    #more code 

def get_today() 
    #the function with return statement 

main() 
+0

刪除d提示我一個新的錯誤TypeError:get_today()缺少1需要的位置參數:'d'' – dezz

+0

您是否從'def get_today()'中移除了'd'? –

+0

是的,我會發布原始問題中的編輯內容。 @Brandon Molyneaux – dezz

相關問題