lesson1:Python 判断表达式应用

1/5/2016 判断表达式应用

Sample1:

salary=raw_input("please input your salary: ")

if salary>=10000:

 print "you can eat twice!^_^"

elif 5000<= salary <10000:

 print "you only can eat once!-_-"

else salary<5000:

 print "you can not eat anything!#_#!"

issue1: salary 起始赋值为string类型,而后面的判断值为整型,需要在判断表达式前进行数据类型转换,salary=int(salary).

Issue2: salary 的str类型在未被转换成整型前,始终会执行第一个分支语句,那是因为string类型始终比int类型大.

Issue3: 在最后一个分支判断上,直接使用else语句,无需添加任何条件,若在else后添加条件,则会出现语法错误,而上例中else :=elif salary<5000:

原文地址:https://www.cnblogs.com/janicce-zhong/p/5104147.html