Python无效语法:Print和Elif语句

Python无效语法:Print和Elif语句,python,syntax,Python,Syntax,我对Python和编程非常陌生(确切地说是2天)。我在闲逛,试图想出一个比打印“Hello world”更复杂的过程。我想知道你们中是否有人能告诉我为什么它会将我的打印和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,

我对Python和编程非常陌生(确切地说是2天)。我在闲逛,试图想出一个比打印“Hello world”更复杂的过程。我想知道你们中是否有人能告诉我为什么它会将我的打印和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 $4.00"
    elif B >= 50
        print "Your shipping is $8.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 $4.00"
elif B >= 50:
    print "Your shipping is $8.00"
else:
    print "Congrats, your shipping is free!"

关于python,您可能首先注意到的是一致性缩进不仅仅是一个好主意,它是强制性的。有经验的程序员,不管他们是否编写Python,总是这样做,所以这没什么大不了的。使用空格(通常是4个空格)并避免制表符-更改编辑器以将制表符替换为4个空格

由于四舍五入的原因,在货币金额上使用浮动是个坏主意。最好使用大的类型,比如Decimal,或者将金额存储为以美分为单位的整数,然后在显示时插入小数点。为了简单起见,我一直坚持使用
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 $8.00"
    amount += 8           # This adds 8 to the amount
elif amount >= 25:
    print "Your shipping is $4.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)
你还有很多事要做。也许可以处理用户输入小写或混合大小写的国家名称-并问问自己,这个问题对用户来说是否合乎逻辑

稍后,您可能需要一个有效国家/地区的列表,并在中使用
来测试用户是否输入了有效的国家/地区。然后将其展开以使用字典,指出每个国家的货币符号、装运金额和货币兑换率


享受巨蟒

一,。缩进,2<代码>如果A==“美国”或A==“加拿大”:
3。忘了把
B=
行的括号括起来。如果你自己做的话,对于一个2天的学习者来说,这是非常了不起的。我得说我建议你下一步尝试用Python解决一些数学问题。最后,你可以用它在学校的数学作业上作弊(当然你不会这么做……)多谢了,你能解释一下B=线上float和int的区别吗?谢谢@杰克,
int()
只接受整数,也就是整数。如果你给它一个像
'23.4'
这样的字符串,它实际上会引发一个
float()
也将接受十进制数字字符串,例如
'23.4'
。由于四舍五入,在货币金额上使用float不好。最好使用大的类型,比如Decimal,或者将金额存储为以美分为单位的整数,然后在显示时插入小数点。再看@cdarke,你是对的当然,我只是不想压倒他。总有一天他会自然而然地做到这一点的。杰克:如果你愿意接受这个回答,按照礼节,别忘了把它标记为接受。只是想帮忙。