Python 3 - 为什么我的简单程序不会对输入做任何事情?

时间:2017-08-26 13:39:23

标签: python python-3.x calculator

我刚刚开始学习Python 3.所以我学到了一些基础知识,并尝试自己写一些东西。它有点计算,但在用户输入后没有任何反应,它只是结束。对不起,如果这是一个非常愚蠢的问题。提前致谢

print("Welcome")
n1 = float(input("Please insert a number"))
o1 = input("Please insert the operator(+,-,*,/)")
n2 = float(input("Please insert another number"))

def mult(x, y):
    z = x * y
    return z


def addi(x, y):
    z = x + y
    return z


def subi(x, y):
    z = x - y
    return z


def divi(x, y):
    if x == 0 or y == 0:
        print("Cant divide 0")
    elif x == 0 and y == 0:
        print("Cant divide 0")
    else:
        z = x / y
        return z


if o1 == "+":
    addi(n1, n2)
elif o1 == "-":
    subi(n1, n2)
elif o1 == "*":
    mult(n1, n2)
elif o1 == "/":
    divi(n1, n2)
else:
    print("Wrong Operator!")
编辑:感谢你们所有人,我修复了它并且它有效。再次感谢。

3 个答案:

答案 0 :(得分:2)

你正在进行计算,但没有输出它们:

if o1 == "+":
    print(addi(n1, n2))
elif o1 == "-":
   print(subi(n1, n2))
elif o1 == "*":
   print(mult(n1, n2))
elif o1 == "/":
   print(divi(n1, n2))
else:
    print("Wrong Operator!")

注意:
您的divi函数中存在错误,请改用:

def divi(x, y):
    if y == 0:
        print("Cant divide by 0")
    else:
        z = x / y
        return z

+ 您只需返回结果而不将其存储在变量中:

def mult(x, y):
    return x * y
def addi(x, y):
    return x + y
def subi(x, y):
    return x - y
def divi(x, y):
    if y == 0:
        print("Cant divide by 0")
    else:
        return x / y

答案 1 :(得分:0)

完美的工作可能是你只想打印结果 添加打印

print("Welcome")
n1 = float(input("Please insert a number"))
o1 = input("Please insert the operator(+,-,*,/)")
n2 = float(input("Please insert another number"))

def mult(x, y):
    z = x * y
    return z


def addi(x, y):
    z = x + y
    return z


def subi(x, y):
    z = x - y
    return z


def divi(x, y):
    if x == 0 or y == 0:
        print("Cant divide 0")
    elif x == 0 and y == 0:
        print("Cant divide 0")
    else:
        z = x / y
        return z


if o1 == "+":
    print(addi(n1, n2))
elif o1 == "-":
    print(subi(n1, n2))
elif o1 == "*":
    print(mult(n1, n2))
elif o1 == "/":
    print(divi(n1, n2))
else:
    print("Wrong Operator!")

输出

欢迎

Please insert a number34
Please insert the operator(+,-,*,/)+
Please insert another number23
57.0

答案 2 :(得分:0)

您计算操作结果,但无论如何都要使用它 - 因此它不会改变程序状态(例如不写入标准输出)。

见下:

if o1 == "+":
    addi(n1, n2)  # addi return value not used
elif o1 == "-":
    subi(n1, n2)  # subi return value not used
elif o1 == "*":
    mult(n1, n2)  # mult return value not used
elif o1 == "/":
    divi(n1, n2)  # divi return value not used
else:
    print("Wrong Operator!")

如您所见 - 返回未使用的值。

让我们看看功能

def mult(x, y):
    z = x * y
    return z


def addi(x, y):
    z = x + y
    return z


def subi(x, y):
    z = x - y
    return z


def divi(x, y):
    if x == 0 or y == 0:
        print("Cant divide 0")
    elif x == 0 and y == 0:
        print("Cant divide 0")
    else:
        z = x / y
        return z

这只是返回值,但无论如何都不输出。

所以你需要打印返回值。例如。 -

if o1 == "+":
    print(addi(n1, n2))
elif o1 == "-":
    print(subi(n1, n2))
elif o1 == "*":
    print(mult(n1, n2))
elif o1 == "/":
    print(divi(n1, n2))
else:
    print("Wrong Operator!")