使用python3的简单计算器

时间:2015-03-27 03:44:43

标签: python-3.x functional-programming

我试图用这种类型的代码制作一个简单的计算器。即使操作应该由用户提供/选择,它仍然继续给我继续操作。我该怎么办? :(

def calc():
    if mode == "1":
        print("Addition")
        main = add()
    elif mode == "2":
        print("Subtraction")
        main = sub()
    elif mode == "3":
        print("Multiplication")
        main = mult()
    elif mode == "4":
        print("Division")
        main = div()
        print("Quotient is equal to", main)
    elif mode == "5":
        print("Modulo")
        main = mod()
    elif mode == "6":
        print("Raise a number to an exponent")
        main = exp()
    elif mode == "7":
        print("Square root")
        main = sq()


def add():
    x = float(input("Enter first number: "))
    y = float(input("Enter second number: "))
    mad = x+y
    print("Sum is equal to", mad)
    return
def sub():
    x = float(input("Enter first number: "))
    y = float(input("Enter second number: "))
    mad = x-y
    print("Difference is equal to", mad)
    return
def mult():
    x = float(input("Enter first number: "))
    y = float(input("Enter second number: "))
    mad = x*y
    print("Product is equal to", mad)
    return
def div():
    x = float(input("Enter first number: "))
    y = float(input("Enter second number: "))
    mad = x/y
    print("Quotient is equal to", mad)
    return
def mod():
    x = float(input("Enter first number: "))
    y = float(input("Enter second number: "))
    mad = x%y
    print("Remainder is equal to", mad)
    return
def exp():
    x = float(input("Enter first number: "))
    y = float(input("Enter second number: "))
    mad = x**y
    print(x, "raised to", y, "is equal to", main)
    return
def sq():
    x = float(input("Enter first number: "))
    mad = x**0.5
    print("Square root of", x, "is", mad)
    return

print("1. Add")
print("2. Subtract")
print("3. Multiply")
print("4. Division")
print("5. Modulo")
print("6. Exponent")
print("7. Square root")
print("8. Exit")
mode = input("What do you want to do? ")

calc()
add()
sub()
mult()
div()
mod()
exp()
sq()

它不断向我询问输入数字,然后不断为所有操作提供一个接一个的答案。

1 个答案:

答案 0 :(得分:0)

你的问题是你在调用cal()后调用了每个函数。你应该评论其他行。此外,你在exp上调用main而不是mad。我猜你想做以下事情:

def calc():
    if mode == "1":
        print("Addition")
        main = add()
    elif mode == "2":
        print("Subtraction")
        main = sub()
    elif mode == "3":
        print("Multiplication")
        main = mult()
    elif mode == "4":
        print("Division")
        main = div()
        print("Quotient is equal to", main)
    elif mode == "5":
        print("Modulo")
        main = mod()
    elif mode == "6":
        print("Raise a number to an exponent")
        main = exp()
    elif mode == "7":
        print("Square root")
        main = sq()


def add():
    x = float(input("Enter first number: "))
    y = float(input("Enter second number: "))
    mad = x+y
    print("Sum is equal to", mad)
    return
def sub():
    x = float(input("Enter first number: "))
    y = float(input("Enter second number: "))
    mad = x-y
    print("Difference is equal to", mad)
    return
def mult():
    x = float(input("Enter first number: "))
    y = float(input("Enter second number: "))
    mad = x*y
    print("Product is equal to", mad)
    return
def div():
    x = float(input("Enter first number: "))
    y = float(input("Enter second number: "))
    mad = x/y
    print("Quotient is equal to", mad)
    return
def mod():
    x = float(input("Enter first number: "))
    y = float(input("Enter second number: "))
    mad = x%y
    print("Remainder is equal to", mad)
    return
def exp():
    x = float(input("Enter first number: "))
    y = float(input("Enter second number: "))
    mad = x**y
    print(x, "raised to", y, "is equal to", mad)
    return
def sq():
    x = float(input("Enter first number: "))
    mad = x**0.5
    print("Square root of", x, "is", mad)
    return

mode = input("What do you want to do? ")
calc()

"""print("1. Add")
print("2. Subtract")
print("3. Multiply")
print("4. Division")
print("5. Modulo")
print("6. Exponent")
print("7. Square root")
print("8. Exit")



add()
sub()
mult()
div()
mod()
exp()
sq()"""