Python学习笔记(3)
程序员文章站
2023-12-24 20:44:15
...
for循环
输出
- break 跳出循环
- continue 跳过本次循环
- exit 退出程序
"""
break:跳出整个循环,不会再循环后面的内容
continue:跳出本次循环,continue后面的代码不再执行,但是循环依然继续
exit():结束程序的运行
"""
for i in range(10):
if i == 5:
# break
# continue
exit()
print('hello python')
print(i)
print('hello world')
测试continue
输出
测试break
输出
while循环
while 条件:
条件满足时,动作1
条件满足时,动作2
练习:
测试
练习:
输出
练习:
"""
猜数字游戏
1. 系统随机生成一个1~100的数字;
2. 用户总共有5次猜数字的机会;
3. 如果用户猜测的数字大于系统给出的数字,打印“too big”;
4. 如果用户猜测的数字小于系统给出的数字,打印"too small";
5. 如果用户猜测的数字等于系统给出的数字,打印"恭喜",并且退
出循环;
"""
import random
trycount = 0
computer = random.randint(1,100)
print(computer)
while trycount < 5:
player = int(input('Num:'))
if player > computer:
print('too big')
trycount += 1
elif player < computer:
print('too small')
trycount += 1
else:
print('Congratulation!')
break
测试
string
索引index
输出
剪切cut
输出
重复
输出
连接
输出
判断是否存在
输出
练习: 判断变量名合法性
测试输出
练习: 回文数
测试输出