此python代码中存在错误
这是我在Python2.7中创建的代码,但它不起作用。我想有个缩进错误。你能帮我吗?按照如下所示,在每个此python代码中存在错误,python,Python,这是我在Python2.7中创建的代码,但它不起作用。我想有个缩进错误。你能帮我吗?按照如下所示,在每个if语句后面添加一个冒号,并将user\u input==input(':')更改为user\u input=input(':')): 编辑: 下面是一个更好的代码版本,它修复了一些错误,比如读取字符串输入、避免被零除异常和删除float()类型转换,因为在python 2.7input()中已经为您做了这些 while True: print ("Options") prin
if语句后面添加一个冒号,并将user\u input==input(':')
更改为user\u input=input(':'))
:
编辑:
下面是一个更好的代码版本,它修复了一些错误,比如读取字符串输入、避免被零除异常和删除float()
类型转换,因为在python 2.7input()
中已经为您做了这些
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)
正如其他用户所建议的,这里还有一个改进版本:
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))
是的,这是一个缩进错误
(但也是一个语法错误
,因为您忘记了:
在if
条件之后)。您缩进的代码是否与问题中所示完全相同?是什么让您认为缩进错误使其“无法运行”?你做了什么来修复它?考虑看介绍Python教程。这肯定会有帮助的。为我的错误感到抱歉,我是一名意大利学生,我正在学习这种编程语言……为什么不把重复的“输入一个数字…”放在elif之外呢?将节省6行!如果你这样做,我会投票:我想你也需要使用原始输入。问题说明使用了python-2.7。我将重新编写它,而不是我解决的OP代码的编辑版本,非常感谢:-)对不起我的错误,我是一名意大利学生,我正在学习这种编程语言。。。
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)