【发布时间】:2015-03-26 07:54:04
【问题描述】:
我编写的以下代码是在 python 中制作“if 语句”的课堂练习的一部分,我将其保存为 x.py。
name = raw_input()("Please enter your name:")
print ("Hello", name)
age = int(raw-input("what is your age?"))
print "according to you, your age is", age
print "in a year you will be ", age +1
#if age is >= 21 print "In the state of Massachusetts, you can legally purchase alcoholic drinks"
if age is => 21:
print "In the state of Massachusetts, you can legally purchase alcoholic drinks."
else:
print " something else…"
print "here’s one more thing…"
问题是当我尝试在终端中运行程序时,出现以下错误:
File "x.py", line 7
if age is => 21:
^
SyntaxError: invalid syntax
我尝试重新输入该行,查找条件语句示例,并在 stackoverflow 中搜索类似问题,但找不到任何与我的错误非常接近的错误。由于新英格兰的暴风雪,我也无法联系到我的教授或助教。我在 v10.9.5 的 mac 书上使用 Python 2.7.9。我将不胜感激。
【问题讨论】:
-
它是
>=。你称它为大于或等于,而不是等于或大于,因此首先是>,然后是=。运算符的语法在here 中描述。此外,您不能一个接一个地使用两个运算符(is和>=)。 -
这有很多问题。当你在编程时,一切都必须准确并且拼写完美。这是
raw_input(prompt_str)不是raw_input()(prompt_str)或raw-input。是if x < y或if x is y而不是if x is < y等。 -
将
if age is => 21:行更改为if age >= 21:。没有is并且(正如 Bakuriu 所说),大于号出现在等号之前。互联网上充斥着正确的if语句示例。当你在学习编程时,你需要做的是仔细观察每个字符是否正确。两位炼金术士发现了一些 Python 在您通过此错误后会抱怨的错误。
标签: python-2.7 if-statement syntax