2015-03-27 3 views
0

Я пытаюсь сделать простой калькулятор только с этими типами кода. Он продолжает давать мне продолжение операций, даже если операция должна быть предоставлена ​​/ выбрана пользователем. Что мне делать? :(Простой калькулятор с использованием python3

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

Он продолжает спрашивать меня для ввода чисел затем непрерывно дает ответы на все операции один за другим.

ответ

0

Ваша проблема заключается в том, что вы назвали каждую функцию после того, как вы назвали кал(). .. вы должны прокомментировать другие линии Кроме того, вы назвали основными на ехре вместо безумного я думаю, что вы хотели сделать следующее:

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()""" 
+0

Я не уверен, что вы действительно хотите поплавок отливать й и у переменных при вычисляя «Modulo». Я думаю, вы должны принимать только типы int – dgsleeps

Смежные вопросы