2016-07-03 14 views
-4

私はこの「世界」の中でここにいます。 Pythonで電卓を作成しようとしましたが、ここにコードがあります。 私はそれを実行しようとすると、IDLEは私にエラーを与える、あなたは私を助けることができますか? :DPythonで作成された電卓

ヘッダ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

問題は、ここではbreak文である:

if user_input == "quit": 
    break 

whileや/またはforループ内でのみbreak文を使用できます。 解決策は、break文をprint()またはexit()で置き換えることです。あなたのプログラムをより効果的にする(とbreakステートメントを使用できるようにする)のために

、あなたは、whileループ内で、あなたの場合は、のelifとelse文を実行できます。

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