Python 困惑,不知什么原因不断地给我一个错误

Python 困惑,不知什么原因不断地给我一个错误,python,Python,上面你可以看到我的代码。当我输入1时输出运行,从我认为它应该运行乘法函数,如果我输入2,它应该运行除数函数。但是我不断地犯错误。我有什么不明白的地方吗。我得到的错误是 bilal = input("Please enter a number") if bilal == 1: print(multiplcation()) elif bilal==2: print(divison()) else: print(rasied_to_the_power())

上面你可以看到我的代码。当我输入1时输出运行,从我认为它应该运行乘法函数,如果我输入2,它应该运行除数函数。但是我不断地犯错误。我有什么不明白的地方吗。我得到的错误是

bilal = input("Please enter a number")

if bilal == 1:
    print(multiplcation())
elif bilal==2:
    print(divison())
else:
    print(rasied_to_the_power())

def multiplcation(a=int(input("enter a number :")),b= int(input("enter a number :"))):
    return a*b
print(multiplcation())

def divison(a=int(input("enter a number :")),b= int(input("enter a number :"))):
    return a/b
print(divison())


def rasied_to_the_power(a=int(input("enter a number :")),b= int(input("enter a number :"))):
    return a**b
print(rasied_to_the_power())
请输入一个数字1
回溯(最近一次呼叫最后一次):
文件“”,第6行,在
NameError:未定义名称“divison”
以下是输出的两个链接:


Python是自上而下评估的。您需要在使用函数之前定义它们

对于第二个错误,您定义了期望输入的函数
(a,b)
。您在没有输入的情况下调用了每个函数

bilal=input(“请输入一个数字”)
更改为
bilal=int(input(“请输入一个数字”))
,因为您正在将值检查为整数,并在代码上方写入函数,因为python从上到下读取它

请用正确的方式写函数

Please enter a number 1
Traceback (most recent call last):
  File "<string>", line 6, in <module>
NameError: name 'divison' is not defined
与其那样写,不如这样写

def multiplcation(a=int(input("enter a number :")),b= int(input("enter a number :"))):
    return a*b
这三个函数都是一样的

以下是全部编辑的代码,请改用它:

  def multiplcation():
    a=int(input("enter a number :"))
    b= int(input("enter a number :"))
    return a*b

请添加错误也请使用完整的回溯添加错误我添加了错误!你为什么要把input()函数作为默认参数?你需要把函数放在你调用它们的行的上方……嗯,我试过了,但还是不起作用。我可以通过编辑我的原始问题来发布图片。您可以自己查看输出。将所有def用法移到文件顶部。将所有打印行和
bilal=
放在下方根据图像,我解决了您的错误。这并不意味着你的代码没有其他的逻辑问题来阻止你期望的输出。我是python新手,我的代码可能有很多逻辑问题,但是我仍然不喜欢让代码按照我想要的方式运行,即使在将def移到文件顶部之后!第一幅图像中的代码是正确的,如果int(bilal)==1,则只需更改
。对于第二个错误,删除每个函数参数列表中的
a,b
,仍然不起作用@米拉特:你再也不会收到你发布的错误了!很好。如果您还需要其他内容,请编辑您的问题,使其包含错误未解决的内容,否则会显示函数未定义!整个代码在问题的顶部,这就是代码@mirratrashid我在答案中包含了全部代码(现在快乐编码;)如果有效,请将其标记为正确答案:))
def multiplcation():
    a=int(input("enter a number :"))
    b= int(input("enter a number :"))
    return a*b

def divison():
    a=int(input("enter a number :"))
    b= int(input("enter a number :"))
    return a/b

def rasied_to_the_power():
    a=int(input("enter a number :"))
    b= int(input("enter a number :"))
    return a**b

bilal = int(input("Please enter a number"))

if bilal == 1:
    print(multiplcation())
elif bilal==2:
    print(divison())
else:
    print(rasied_to_the_power())