python学习——条件判断
  TEZNKK3IfmPf 2023年11月15日 17 0

条件判断

计算机之所以能做很多自动化的任务,因为它可以自己做条件判断。

比如,输入用户年龄,根据年龄打印不同的内容,在Python程序中,用if语句实现:

age = 20if age >= 18:    print('your age is', age)    print('adult')

根据Python的缩进规则,如果if语句判断是True,就把缩进的两行print语句执行了,否则,什么也不做。

也可以给if添加一个else语句,意思是,如果if判断是False,不要执行if的内容,去把else执行了:

age = 3if age >= 18:    print('your age is', age)    print('adult')else:    print('your age is', age)    print('teenager')

注意不要少写了冒号:

当然上面的判断是很粗略的,完全可以用elif做更细致的判断:

age = 3if age >= 18:
    print('adult')elif age >= 6:
    print('teenager')else:
    print('kid')

elifelse if的缩写,完全可以有多个elif,所以if语句的完整形式就是:

if <条件判断1>:    <执行1>elif <条件判断2>:    <执行2>elif <条件判断3>:    <执行3>else:    <执行4>

if语句执行有个特点&#xff0c;它是从上往下判断&#xff0c;如果在某个判断上是True&#xff0c;把该判断对应的语句执行后&#xff0c;就忽略掉剩下的elifelse&#xff0c;所以&#xff0c;请测试并解释为什么下面的程序打印的是teenager&#xff1a;

age = 20if age >= 6:
    print('teenager')elif age >= 18:
    print('adult')else:
    print('kid')

if判断条件还可以简写&#xff0c;比如写&#xff1a;

if x:    print('True')

只要x是非零数值、非空字符串、非空list等&#xff0c;就判断为True&#xff0c;否则为False

再议 input

最后看一个有问题的条件判断。很多同学会用input()读取用户的输入&#xff0c;这样可以自己输入&#xff0c;程序运行得更有意思&#xff1a;

birth = input('birth: ')if birth < 2000:    print('00前')else:    print('00后')

输入1982&#xff0c;结果报错&#xff1a;

Traceback (most recent call last):
  File "<stdin>", line 1, in <module>TypeError: unorderable types: str() > int()

这是因为input()返回的数据类型是str&#xff0c;str不能直接和整数比较&#xff0c;必须先把str转换成整数。Python提供了int()函数来完成这件事情&#xff1a;

s = input('birth: ')
birth = int(s)if birth < 2000:    print('00前')else:    print('00后')

再次运行&#xff0c;就可以得到正确地结果。但是&#xff0c;如果输入abc呢&#xff1f;又会得到一个错误信息&#xff1a;

Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
ValueError: invalid literal for int() with base 10: 'abc'

原来int()函数发现一个字符串并不是合法的数字时就会报错&#xff0c;程序就退出了。

【版权声明】本文内容来自摩杜云社区用户原创、第三方投稿、转载,内容版权归原作者所有。本网站的目的在于传递更多信息,不拥有版权,亦不承担相应法律责任。如果您发现本社区中有涉嫌抄袭的内容,欢迎发送邮件进行举报,并提供相关证据,一经查实,本社区将立刻删除涉嫌侵权内容,举报邮箱: cloudbbs@moduyun.com

  1. 分享:
最后一次编辑于 2023年11月15日 0

暂无评论

推荐阅读
  TEZNKK3IfmPf   2024年05月31日   34   0   0 python开发语言
  TEZNKK3IfmPf   2024年05月31日   25   0   0 python
  TEZNKK3IfmPf   2024年05月31日   34   0   0 excelpython
  TEZNKK3IfmPf   2024年05月31日   27   0   0 python
TEZNKK3IfmPf