虚构售货机,所谓虚构是因为计算机不会真的给我们商品,下面开始编程吧:
补充下elif使用情况:
当判断条件多余2个时;想代替switch case 时;
1.创建文件;
2.输入:
print "a: Cake"
print "b: Carrot"
print "c: Fish"
print "d: Soup"
3.输入:choice = raw_input("Select an option (a,b,c or d)")
4.输入:(同样注意print前的四个空格)
if choice == "a":
print "Here is your cake. Yum!"
elif choice == "b": #需要多余两个判断时,可以用elif
print "Carrots are orange. Have some."
elif choice == "c":
print "Fish live in water.Enjoy!"
elif choice == "d":
print "Today's soup is tomato.Spoons are located behind you."
else:
print choice + " is not a valid option. Have some air."
这就算是结束了,按下F5试一下吧;