开发者学堂课程【Python 入门 2020年版:Pass 关键字的使用】学习笔记,与课程紧密联系,让用户快速学习知识。
课程地址:https://developer.aliyun.com/learning/course/639/detail/10263
Pass 关键字的使用
Pass 关键字的使用
#pass 是什么?
pass 关键字在 python 里没有意义,只是单纯的用来占位,保证语句的完整性。
情况一:
age = int(input(‘请输入您的年龄:’)
if age >18:
print(‘欢迎来到我的网站’)
print(‘hello’)
问题:hello 什么时候会打印?
运行结果1:C:\Users\chris\AppData\Local\Programs\Python\Python37\python.exe C:/Users/chris/Desktop/Python i/Day04-流程
请输入您的年龄:23
Hello
Process finished with exit code 0
运行结果2:
结论:都会打印 hello。因为 print 和 hello 跟 if 语句是不受if语句的控制。是通过换行(强制缩进)来控制。
情况二:增加个 yes
age = int(input(‘请输入您的年龄:’)
if age >18:
print(‘欢迎来到我的网站’)
print(‘yes’)
print(‘hello’)
运行结果:C:\Users\chris\AppData\Local\Programs\Python\Python37\python.exe C:/Users/chris/Desktop/Python i/Day04-流程
请输入您的年龄:12
hello
Process finished with exit code 0
结论:
yes 什么时候会打印?
yes 跟 if 有没有关系?yes 跟 age 有没与关系?yes 满足只有条件才会打印,不满足条件就不会打印对应得代码。
情况三:
age = int(input(‘请输入您的年龄:’)
if age >18:
pass
# pass #没有想好写什么,使用 pass 进行占位,没有意义,单纯为了保证语句的完整性,使程序不报错
print(‘hello’)
应用游戏:剪刀石头布
<1>运行效果:
[pythongubuntu:~/Desktop$ python test.py
[请输入:剪刀(0)石头(1)布(2):1
输了,不要走,洗洗手接着来,决战到天亮
[pythongubuntu:~/Desktop$ python test.py
[请输入:剪刀(0)石头(1)布(2):1
平局,要不再来一局
[pythongubuntu:~/Desktop$ python test.py
[请输入:剪刀(0)石头(1)布(2):1
输了,不要走,洗洗手接着来,决战到天亮
<2>参考代码:
Import random
player = input(‘请输入:剪刀(0)石头(1)布(2):’)
player = int(player)
#产生随机整数:0、1、2、中的某一个
computer = random.randint(0,2)
#用来进行测试
#print(‘player=%d,computer=%d,(player,computer))
If ((player ==0) and (computer == 2)) or ((player ==1) and
(computer ==0)) or ((player ==2) and
Print(‘获胜,哈哈,你太厉害了’)
elif player == computer:
print(‘平局,要不再来一局’)
else:
print(‘输了,不要走,洗洗手接着来,决战到天亮’)