我刚刚启动了新版本的python,并意识到已经发生了很多变化。无论如何,eclipse在行号旁边出现一个红色的“X”标记,上面写着“Expected ::”。有人可以解释这意味着什么,以及如何摆脱它?
这是我正在尝试使用Eclipse和新Python版本的代码:
print "Please insert a valid operator that you want to calculate with."
print "Valid operators are +, -, : and *"
operator = str(raw_input("What's your operator? "))
numb1 = int(raw_input("Please insert the first number:"))
numb2 = int(raw_input("Please insert the second number:"))
if operator == "+":
print numb1 + numb2
elif operator == "*":
print numb1 + numb2
elif operator == "-":
print numb1 - numb2
elif operator == "/":
print numb1 / numb2
答案 0 :(得分:1)
在Python3上,print
是一个函数,而不是一个语句,因此它应该被编写(例如)
print("Please insert a valid operator that you want to calculate with.")
此外raw_input
已重命名为input
,因此应该(例如):
numb1 = int(input("Please insert the first number:"))
答案 1 :(得分:0)
我运行了这个程序,当我在Eclipse上运行Pydev时,我发现它没有问题,即使我在2.7上运行它。也许它与你的缩进有关。
operator = str(raw_input("What's your operator? "))
numb1 = int(raw_input("Please insert the first number:"))
numb2 = int(raw_input("Please insert the second number:"))
if operator == "+":
print numb1 + numb2
elif operator == "*":
print numb1 + numb2
elif operator == "-":
print numb1 - numb2
elif operator == "/":
print numb1 / numb2
答案 2 :(得分:0)
我复制粘贴你的例子并修复它我只需要像StackXchangeT那样修复缩进。
但是,如果在声明结束时缺少完成Expected::
,则会出现:
错误,例如:
class MyInvalidClass
应该是:
class MyInvalidClass:
在类似需要:
的情况下(或者只是猜测),也许你会遇到这样的错误。