Python2.7上未出现的输出
所以,我在用Python2.7编写一个程序,结果没有显示出来。以下是源代码:Python2.7上未出现的输出,python,Python,所以,我在用Python2.7编写一个程序,结果没有显示出来。以下是源代码: print("What operator do you want to use?") operator = input() print("Enter the first value") val1 = int(input()) print("Enter the second value") val2 = int(input()) if val1 == 45 an
print("What operator do you want to use?")
operator = input()
print("Enter the first value")
val1 = int(input())
print("Enter the second value")
val2 = int(input())
if val1 == 45 and val2 == 3 and operator == "*":
print(555)
if val1 == 56 and val2 == 9 and operator == "+":
print(77)
if val1 == 56 and val2 == 6 and operator == "/":
print(4)
elif operator == "/":
i = val1/val2
print(i)
elif operator == "*":
j = val1*val2
print(j)
elif operator == "+":
k = val1+val2
print(k)
elif operator == "-":
l = val1-val2
print(l)
以下是我的意见:
What operator do you want to use?
"+"
Enter the first value
20
Enter the second value
20
Process finished with exit code 0
结果应该出现的地方,只有一个空白,如果我输入的运算符没有引号,则会发生此错误:
Traceback (most recent call last): File "C:/Users/USER/PycharmProjects/newPythonProject/Tests.py", line 2, in <module> operator = input() File "<string>", line 1 + ^ SyntaxError: unexpected EOF while parsing
Traceback(最后一次调用):文件“C:/Users/USER/PycharmProjects/newPythonProject/Tests.py”,第2行,operator=input()文件“”,第1行+^syntaxer错误:解析时出现意外的EOF
您在输入中为运算符添加了引号。在python 3.x中,您似乎在引号中输入了运算符。input()
函数不解释,它总是返回字符串
然而,在Python2.x中,input()
函数返回最可能的数据类型,sloppily说。由于您希望输入为字符串,因此可以使用raw\u input
,它不会解释输入,而是始终返回字符串
也就是说,您的输入已经是一个字符串。因此,引号“”不仅仅是多余的,它们还改变了输入字符串
在您的例子中,由于python 2.7解释其输入,因此强制转换int(input())
本身并不必要,因为input()
将自动假定您的输入20
属于int
类型
使用下面的代码,您的函数应该可以工作
print("What operator do you want to use?")
operator = raw_input()
print("Enter the first value")
val1 = int(input())
print("Enter the second value")
val2 = int(input())
if val1 == 45 and val2 == 3 and operator == "*":
print(555)
if val1 == 56 and val2 == 9 and operator == "+":
print(77)
if val1 == 56 and val2 == 6 and operator == "/":
print(4)
elif operator == "/":
i = val1/val2
print(i)
elif operator == "*":
j = val1*val2
print(j)
elif operator == "+":
k = val1+val2
print(k)
elif operator == "-":
l = val1-val2
print(l)
具有以下输入:
What operator do you want to use?
+
Enter the first value
20
Enter the second value
20
键入
+
,而不是带引号的“+”
。然后将消息作为input(“您想使用什么操作符?”)输入到输入法中。
您只需要一行,就可以去掉printYa,但这就是我在不输入命令时得到的结果:“回溯(最后一次调用):文件“C:/Users/USER/PycharmProjects/newPythonProject/Tests.py”,第2行,在operator=input()文件“”中,第1行+^SyntaxError:operator=input()文件“”中的第2行,第1行+^SyntaxError:operator=input()文件“”中的意外EOF。你可以用raw_input
而不是input来解决你的问题。
我会在这个问题上修改我的答案。是的,但这就是我在不提出问题的情况下得到的结果:“回溯(最后一次调用):文件“C:/Users/USER/PycharmProjects/newPythonProject/Tests.py”,第2行,在operator=input()文件“”中,第1行+^语法错误:分析“”时出现意外的EOF