尝试调用函数时出错

时间:2017-10-08 02:38:45

标签: python function

我是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 个答案:

答案 0 :(得分: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()