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

2024-06-01 07:56:25 发布

您现在位置:Python中文网/ 问答频道 /正文

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时输出运行,从我认为它应该运行乘法函数,如果我输入2,它应该运行除数函数。但是我不断地犯错误。我有什么不明白的地方吗。我得到的错误是

Please enter a number 1
Traceback (most recent call last):
  File "<string>", line 6, in <module>
NameError: name 'divison' is not defined

以下是输出的两个链接:

enter image description here

enter image description here


Tags: thetonumberinputreturndefintpower
2条回答

bilal = input("Please enter a number")更改为bilal = int(input("Please enter a number")),因为您正在将值检查为整数,并在代码上方编写函数,因为python自上而下读取它

请用正确的方式写函数

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

这三个函数都是一样的

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

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())

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

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

相关问题 更多 >