1、基本if语句(尤其要注意缩进!)
answer="YES"
answerInput=input("would you like eat pork ?")
if answerInput.upper() == answer.upper(): #技巧:两边使用同样的函数,解决在不能确定大小写或者忘记大小写, #而且无需区分大小写的情况
print("OK!")
print("10 yuan!") #缩进是必要的!试验去掉缩进的情况!
print("please do yourself!")
answer=input("Would you like express shipping?")
if answer.lower() == "yes" : #注意最后的 “:”
print("That will be an extra $10")
print("Have a nice day")
2、Ctrl+k+c:Python注释多行
3、处理用户输入
deposit=input("How much would you like to deposit? ")
if deposit > 100 : #错误!str不能和int比较!
print("You get a free toaster!")
print("Have a nice day")
解决办法(1):
deposit=input("How much would you like to deposit? ")
if int(deposit) > 100 :
print("You get a free toaster!")
print("Have a nice day")
解决办法(2):
deposit=int(input("How much would you like to deposit? "))
if deposit > 100 :
print("You get a free toaster!")
print("Have a nice day")
4、if 、else
deposit=input("How much would you like to deposit? ")
if int(deposit) > 100:
print("You get a free toaster!")
else:
print("Enjoy your mug!")
print("Have a nice day!")
输入50、100、150
5、if、elif、elif......
country = input("Where are you from? " )
if country == "CANADA" :
print("Hello")
elif country == "GERMANY" :
print("Guten Tag")
elif country == "FRANCE" :
print("Bonjour")
else :
print("Aloha/Ciao/G’Day")
6、if and
6、if or
7、if and or 混合使用!
最好是添加括号,显式区别优先级!或者不要混合使用and、or!
if country == "CANADA" and (pet == "MOOSE" or pet == "BEAVER") :
print("Do you play hockey too")
8、嵌套
monday = True
freshCoffee = False
if monday :
if not freshCoffee :
print("go buy a coffee!")
print("I hate Mondays")
print("now you can start work") #尤其注意这些缩进,变换一下,试验一下各种缩进的情况