2017-07-17 57 views
-6
while True: 
print ("Options") 
print ("Write 'Quit' if you want to exit") 
print ("Write '+'if you want to make an addition") 
print ("Write '-' if you want to make a sottration") 
print ("Write '*' if you want to make a moltiplication") 
print ("Write '/' if you wantto make a division") 
user_input == input(":") 
     if user_input == ("+") 
     num1 = float(input("Enter a number...") 
     num2 = float(input("Enter the second number...") 
     result = str(num1+num2) 
     print("The result is"+ result) 
     elif user_input == ("-") 
     num1 = float(input("Enter a number...") 
     num2 = float(input("Enter the second number...") 
     result = str(num1-num2) 
     print("The result is"+ result) 
     elif user_input == ("*") 
     num1 = float(input("Enter a number...") 
     num2 = float(input("Enter the second number...") 
     result = str(num1*num2) 
     print("The result is"+ result) 
     elif user_input == ("/") 
     num1 = float(input("Enter a number...") 
     num2 = float(input("Enter the second number...") 
     print ("The result is"+ result) 

这是我在python 2.7中创建的代码,但它不起作用。我认为存在缩进错误。你可以帮我吗?此python代码中的错误

+5

是的,这是一个'IndentationError'(也有'SyntaxError'因为你忘了':'了'如果后'条件)。您的代码是否正好如问题中所示缩进? – MSeifert

+3

是什么让你觉得有一个缩进错误让它“不起作用”?你做了什么试图解决它? – Sayse

+0

请考虑观看介绍性的Python教程。它一定会有所帮助。 –

回答

2

解决您的压痕和各if-statement后加冒号以下,改变user_input == input(':')到​​:

while True: 
    print ("Options") 
    print ("Write 'Quit' if you want to exit") 
    print ("Write '+'if you want to make an addition") 
    print ("Write '-' if you want to make a sottration") 
    print ("Write '*' if you want to make a moltiplication") 
    print ("Write '/' if you wantto make a division") 
    user_input = input(":") # fix this line 
    if user_input == ("+"): 
     num1 = float(input("Enter a number...")) 
     num2 = float(input("Enter the second number...")) 
     result = str(num1+num2) 
     print("The result is"+ result) 
    elif user_input == ("-"): 
     num1 = float(input("Enter a number...")) 
     num2 = float(input("Enter the second number...")) 
     result = str(num1-num2) 
     print("The result is"+ result) 
    elif user_input == ("*"): 
     num1 = float(input("Enter a number...")) 
     num2 = float(input("Enter the second number...")) 
     result = str(num1*num2) 
     print("The result is"+ result) 
    elif user_input == ("/"): 
     num1 = float(input("Enter a number...")) 
     num2 = float(input("Enter the second number...")) 
     result = str(num1/num2) 
     print ("The result is"+ result) 

编辑:

下面是一个更好版本的代码,修复一些错误,就像读取字符串输入一样,避免除以零异常并删除float()类型的投射,因为在python 2.7 input()已经为你做了。

while True: 
    print("Options") 
    print("Write 'Quit' if you want to exit") 
    print("Write '+'if you want to make an addition") 
    print("Write '-' if you want to make a sottration") 
    print("Write '*' if you want to make a moltiplication") 
    print("Write '/' if you wantto make a division") 
    user_input = raw_input(":") 
    if user_input == '+': 
     num1 = input("Enter a number...") 
     num2 = input("Enter the second number...") 
     print('The result is {}'.format(num1+num2)) 
    elif user_input == '-': 
     num1 = input("Enter a number...") 
     num2 = input("Enter the second number...") 
     print('The result is {}'.format(num1-num2)) 
    elif user_input == '*': 
     num1 = input("Enter a number...") 
     num2 = input("Enter the second number...") 
     print('The result is {}'.format(num1*num2)) 
    elif user_input == '/': 
     num1 = input("Enter a number...") 
     num2 = input("Enter the second number...") 
     if num2 == 0: 
      print("Can't divide by zero.") 
     else: 
      print("The result is {}".format(num1/num2)) 

此外,通过其他用户的建议这里是一个改进版本:

while True: 
    print("Options") 
    print("Write 'Quit' if you want to exit") 
    print("Write '+'if you want to make an addition") 
    print("Write '-' if you want to make a sottration") 
    print("Write '*' if you want to make a moltiplication") 
    print("Write '/' if you wantto make a division") 
    user_input = raw_input(":") 
    num1 = input("Enter a number...") 
    num2 = input("Enter the second number...") 

    if user_input == "+": 
     result = str(num1+num2) 
    elif user_input == "-": 
     result = str(num1-num2) 
    elif user_input == "*": 
     result = str(num1*num2) 
    elif user_input == "/": 
     if num2 == 0: 
      result = "Can't divide by zero" 
     else: 
      result = str(num1/num2) 

    print("The result is", result) 
+2

为什么不把重复的“输入数字...”放在elif之外?会节省6行!如果你这样做,我upvote:D – Fabien

+0

我想你还需要使用'raw_input'。这个问题表明使用了python-2.7。 – MSeifert

+0

我只是重新编写它,而不是编辑版本的OP代码 –