2016-07-03 3 views
-4

Я новичок здесь, в этом «мире». Я попытался создать калькулятор с Python, вот код. Когда я пытаюсь запустить его, IDLE дает мне ошибки, вы можете мне помочь, пожалуйста? .: DКалькулятор, созданный с помощью Python

Заголовок 1

print("Options") 
print("Type 'add' to add two numbers") 
print("Type'subtract' to subtract two numbers") 
print("Type'multiply' to multiply two numbers") 
print("Type'divide' to divide two numbers") 
print("Type'quit' to exit") 
user_input = input(": ") 

if user_input == "quit": 
    break 

elif user_input == "add" : 
    num1 = float(input("Insert a number: ")) 
    num2 = float(input("Insert another number: ")) 
    result = str(num1+num2) 
    print("The answer is " + result) 

elif user_input == "subtract" : 
    num1 = float(input("Insert a number: ")) 
    num2 = float(input("Insert another number: ")) 
    result = str(num1+num2) 
    print("The answer is" + result) 

elif user_input == "multiply" : 
    num1 = float(input("Insert a number: ")) 
    num2 = float(input("Insert another number: ")) 
    result = str(num1+num2) 
    print("The answer is " + result) 

elif user_input == "divide" : 
    num1 = float(input("Insert a number: ")) 
    num2 = float(input("Insert another number: ")) 
    result = str(num1+num2) 
    print("The answer is " + result) 
else: 
    print("Unknown command") 
+0

пожалуйста включают ошибку в вашем вопрос – Creos

ответ

0

Попробуйте это (вы не цикл, так что используйте выход(), а не разрыв, а также использовать правильные операторы для оных, разделяй, суб-структуры, многократно

from __future__ import division # to support division 
print("Options") 
print("Type 'add' to add two numbers") 
print("Type'subtract' to subtract two numbers") 
print("Type'multiply' to multiply two numbers") 
print("Type'divide' to divide two numbers") 
print("Type'quit' to exit") 
user_input = raw_input(": ") 

if user_input == "quit": 
    exit() #break is uesd in loops 

elif user_input == "add" : 
    num1 = float(input("Insert a number: ")) 
    num2 = float(input("Insert another number: ")) 
    result = str(num1+num2) 
    print("The answer is " + result) 

elif user_input == "subtract" : 
    num1 = float(input("Insert a number: ")) 
    num2 = float(input("Insert another number: ")) 
    result = str(num1-num2) 
    print("The answer is" + result) 

elif user_input == "multiply" : 
    num1 = float(input("Insert a number: ")) 
    num2 = float(input("Insert another number: ")) 
    result = str(num1*num2) 
    print("The answer is " + result) 

elif user_input == "divide" : 
    num1 = float(input("Insert a number: ")) 
    num2 = float(input("Insert another number: ")) 
    result = str(num1/num2) 
    print("The answer is " + result) 
else: 
    print("Unknown command") 
+0

Большое спасибо, ребята! : D –

-1
if user_input == 'quit': 
    #break 
    pass 
+0

Это не достигнет того, чего хочет OP. – IanAuld

0

проблема заключается в заявлении перерыв здесь:

if user_input == "quit": 
    break 

Вы можете использовать оператор break только в течение и/или для цикла. Решение заключается в замене оператора break на print() или exit().

для вашей программы, чтобы быть более эффективной (и быть в состоянии использовать оператор разрыва), вы могли бы выполнить свой, если Элиф и другое заявление в цикле в то время:

print("Options") 
print("Type 'add' to add two numbers") 
print("Type'subtract' to subtract two numbers") 
print("Type'multiply' to multiply two numbers") 
print("Type'divide' to divide two numbers") 
print("Type'quit' to exit") 

while True: 
    user_input = input(": ") 
    if user_input == "quit": 
     print('Program terminated') 
     break 

    elif user_input == "add" : 
     num1 = float(input("Insert a number: ")) 
     num2 = float(input("Insert another number: ")) 
     result = str(num1+num2) 
     print("The answer is " + result) 

    elif user_input == "subtract" : 
     num1 = float(input("Insert a number: ")) 
     num2 = float(input("Insert another number: ")) 
     result = str(num1+num2) 
     print("The answer is" + result) 

    elif user_input == "multiply" : 
     num1 = float(input("Insert a number: ")) 
     num2 = float(input("Insert another number: ")) 
     result = str(num1+num2) 
     print("The answer is " + result) 

    elif user_input == "divide" : 
     num1 = float(input("Insert a number: ")) 
     num2 = float(input("Insert another number: ")) 
     result = str(num1+num2) 
     print("The answer is " + result) 
    else: 
     print("Unknown command") 
Смежные вопросы