Python 无效语法:Print 和 Elif 语句
Python Invalid Syntax: Print and Elif statements
我对 Python 和编程真的很陌生(准确地说是 2 天)。我在闲置时试图想出一个比 print "Hello world." 更复杂的过程 我想知道你们中是否有人能告诉我为什么它将我的 print 和 elif 语句标记为无效。我正在使用 Python 2.7.10,谢谢!
A = raw_input("Do you live in the US or Canada?")
if A == " US" or "Canada":
print "Welcome!"
else:
print "We're sorry, but your country is currently not supported!"
B = int(raw_input("How much is your package?")
if B >= 25
print "Your shipping is .00"
elif B >= 50
print "Your shipping is .00"
else:
print "Congrats, your shipping is free!"
已为您修复:)。看看有什么不同,你会学到的,小学徒:
A = raw_input("Do you live in the US or Canada?")
if A == "US" or A == "Canada":
print "Welcome!"
else:
print "We're sorry, but your country is currently not supported!"
B = float(raw_input("How much is your package?"))
if B >= 25:
print "Your shipping is .00"
elif B >= 50:
print "Your shipping is .00"
else:
print "Congrats, your shipping is free!"
您注意到 python 的第一件事可能是一致的缩进不仅是个好主意,而且是强制性的。有经验的程序员,不管他们是否写 Python ,总是会这样做,所以这没什么大不了的。使用空格(4 个是标准)并避免使用制表符 - 更改您的编辑器以用 4 个空格替换制表符。
因为四舍五入,所以在金额上使用浮点数是个坏主意。最好使用大类型,如 Decimal,或将金额存储为以分为单位的 int,然后在显示时插入小数点。为简单起见,我坚持使用 float
,但请注意。
您的代码中有很多逻辑错误,还有样式问题。编程风格不仅仅是关于什么看起来好看,而是当你回过头来时是否能理解你的代码。
风格点:
不要对变量使用大写。按照惯例,大写字母保留给常量
使用有意义的变量名,而不是 A 和 B
这是一个更正的程序,带有注释。请阅读评论! :
# This is a comment, it is ignored by python
# This is used later on by sys.exit()
import sys
# Logically the user would enter "Yes" or "No" to this quesion,
# not US or Canada!
ans = raw_input("Do you live in the US or Canada? ") # Notice the space after ?
# Note how the condition has been expanded
if ans == "US" or ans == "Canada":
print "Welcome!"
else:
print "We're sorry, but your country is currently not supported!"
# Now what? Your program just carried on. This will stop it
sys.exit()
# I'm using a floating point number for simplicity
amount = float(raw_input("How much is your package? "))
# I changed this around, since 50 is also >= 25!
# However, this is strange. Usually the more you spend the LESS the shipping!
# You were missing the : after the condition
if amount >= 50:
print "Your shipping is .00"
amount += 8 # This adds 8 to the amount
elif amount >= 25:
print "Your shipping is .00"
amount += 4 # This adds 4 to the amount
else:
print "Congrats, your shipping is free!"
# print the amount showing 2 decimal places, rounding
print "Amount to pay: $%.2f" % (amount)
你还有很多事要做。也许应对用户为国家名称输入小写或混合大小写字母 - 并问问自己这个问题对用户来说是否合乎逻辑。
稍后您可能想要一个有效国家/地区列表,并使用 in
来测试用户是否输入了有效国家/地区。然后将其扩展为使用字典,指示每个国家/地区的货币符号、运费和货币兑换率。
享受Python!
我对 Python 和编程真的很陌生(准确地说是 2 天)。我在闲置时试图想出一个比 print "Hello world." 更复杂的过程 我想知道你们中是否有人能告诉我为什么它将我的 print 和 elif 语句标记为无效。我正在使用 Python 2.7.10,谢谢!
A = raw_input("Do you live in the US or Canada?")
if A == " US" or "Canada":
print "Welcome!"
else:
print "We're sorry, but your country is currently not supported!"
B = int(raw_input("How much is your package?")
if B >= 25
print "Your shipping is .00"
elif B >= 50
print "Your shipping is .00"
else:
print "Congrats, your shipping is free!"
已为您修复:)。看看有什么不同,你会学到的,小学徒:
A = raw_input("Do you live in the US or Canada?")
if A == "US" or A == "Canada":
print "Welcome!"
else:
print "We're sorry, but your country is currently not supported!"
B = float(raw_input("How much is your package?"))
if B >= 25:
print "Your shipping is .00"
elif B >= 50:
print "Your shipping is .00"
else:
print "Congrats, your shipping is free!"
您注意到 python 的第一件事可能是一致的缩进不仅是个好主意,而且是强制性的。有经验的程序员,不管他们是否写 Python ,总是会这样做,所以这没什么大不了的。使用空格(4 个是标准)并避免使用制表符 - 更改您的编辑器以用 4 个空格替换制表符。
因为四舍五入,所以在金额上使用浮点数是个坏主意。最好使用大类型,如 Decimal,或将金额存储为以分为单位的 int,然后在显示时插入小数点。为简单起见,我坚持使用 float
,但请注意。
您的代码中有很多逻辑错误,还有样式问题。编程风格不仅仅是关于什么看起来好看,而是当你回过头来时是否能理解你的代码。
风格点:
不要对变量使用大写。按照惯例,大写字母保留给常量
使用有意义的变量名,而不是 A 和 B
这是一个更正的程序,带有注释。请阅读评论! :
# This is a comment, it is ignored by python
# This is used later on by sys.exit()
import sys
# Logically the user would enter "Yes" or "No" to this quesion,
# not US or Canada!
ans = raw_input("Do you live in the US or Canada? ") # Notice the space after ?
# Note how the condition has been expanded
if ans == "US" or ans == "Canada":
print "Welcome!"
else:
print "We're sorry, but your country is currently not supported!"
# Now what? Your program just carried on. This will stop it
sys.exit()
# I'm using a floating point number for simplicity
amount = float(raw_input("How much is your package? "))
# I changed this around, since 50 is also >= 25!
# However, this is strange. Usually the more you spend the LESS the shipping!
# You were missing the : after the condition
if amount >= 50:
print "Your shipping is .00"
amount += 8 # This adds 8 to the amount
elif amount >= 25:
print "Your shipping is .00"
amount += 4 # This adds 4 to the amount
else:
print "Congrats, your shipping is free!"
# print the amount showing 2 decimal places, rounding
print "Amount to pay: $%.2f" % (amount)
你还有很多事要做。也许应对用户为国家名称输入小写或混合大小写字母 - 并问问自己这个问题对用户来说是否合乎逻辑。
稍后您可能想要一个有效国家/地区列表,并使用 in
来测试用户是否输入了有效国家/地区。然后将其扩展为使用字典,指示每个国家/地区的货币符号、运费和货币兑换率。
享受Python!