2017-10-08 61 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

相关问题