Python中的for循环,while循环,程序中断介绍
1.灵活使用break,continue,exit
------->题目要求:
break:跳出整个循环,不会再循环后面的内容
continue:跳出本次循环,continue后面的代码不再执行,但是循环依然继续
exit():结束程序的运行
------->代码如下:
for i in range(10):
if i == 5:
# break
continue
print('hello python')
# exit()
print(i)
print('hello world')
------->测试如下:
2.for循环练习
-------><1>题目要求:
用户输入一个整型数,求该数的阶乘,例如5 = 5 * 4 * 3 * 2 * 1
------->代码如下:
num = int(input('Num:'))
res = 1
for i in range(1,num+1):
res *= i
print('%d 的结果是: %d' %(num,res))
------->测试如下:
-------><2>题目要求:
有1,2,3,4四个数字
求这四个数字能生成多少互不相同且无重复数字的三位数(122,133)
------->代码如下:
count = 0
for i in range(1,5):
for j in ran ge(1,5):
for k in range(1,5):
if i != j and j !=k and i != k:
print(i * 100 + j * 10 + k)
count += 1
print('生成%d个无重复的三位数' %count)
------->测试如下:
-------><3>题目要求:
用户登陆程序需求:
1. 输入用户名和密码;
2. 判断用户名和密码是否正确? (name=‘root’, passwd=‘westos’)
3. 登陆仅有三次机会, 如果超>过三次机会, 报错提示;
------->代码如下:
trycount = 0
# for i in range(3):
while trycount < 3:
name = input('用户名: ')
password = input('密码: ')
if name == 'root' and password == 'westos':
print('登录成功')
break
else:
print('登录失败')
print('您还剩余%d次机会' %(2-trycount))
trycount += 1
else:
print('登录次数超过3次,请稍后再试!')
------->测试如下:
3.while循环练习
-------><1>题目要求:
while 条件():
条件满足时,做的事情1
条件满足时,做的事情2
print(‘hello python’)
------->代码如下:
#定义一个变量,记录循环次数
i = 1
#开始循环
while i <= 3:
#循环内执行的动作
print('hello python')
#处理计数器
i += 1
------->测试如下:
-------><2>题目要求:
实行while死循环
------->代码如下:
while True:
print('hello python')
------->测试如下:
-------><3>题目要求:
使用while求1到100的和
------->代码如下:
i = 0
result = 0
while i <= 100:
result += i
i += 1
print('和为:%d' %result)
------->测试如下:
-------><4>题目要求:
在控制台连续输出五行*,每行依次递增
即使用while循环显示出以下图形:
*****
****
***
**
*
------->代码如下:
row = 5
while row >= 1:
col = 1
while col <= row:
print('*',end='')
col += 1
print('')
row -= 1
------->测试如下:
-------><5>题目要求:
以正三角形的形状输出9*9乘法表
------->代码如下:
row = 1
while row <= 9:
col = 1
while col <= row:
print('%d * %d = %d\t' %(row,col,col * row),end='')
col += 1
print('')
row += 1
------->测试如下:
-------><6>题目要求:
猜数字游戏
- 系统随机生成一个1~100的数字;
- 用户总共有5次猜数字的机会;
- 如果用户猜测的数字大于系统给出的数字,打印“too big”;
- 如果用户猜测的数字小于系统给出的数字,打印"too small";
- 如果用户猜测的数字等于系统给出的数字,打印"恭喜",并且退出循环;
------->代码如下:
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('恭喜')
break
------->测试如下:
-------><7>题目要求
在python的IDE中输出类似于shell界面中的内容并且可以成功使用命令
------->代码如下:
import os
for i in range(1000):
cmd = input('[[email protected] gege]$ ')
if cmd:
if cmd == 'exit':
print('logout')
break
else:
print('run %s' %cmd)
os.system(cmd)
else:
continue
------->测试结果如下:
------><8>,题目要求
输入两个数值,求两个数的最大公约数(能被两个数整除的最大的数)和最大公倍数(最小公倍数=(n1*n2)/最大公约数
思路:使用循环,遍历
------->代码如下:
#1.输入两个数
num1 = int(input('Num1:'))
num2 = int(input('Num2:'))
#2.找出两个数中的最小数
min_num = min(num1,num2)
#3.最大公约数的范围在1~min_num之间
for i in range(1,min_num + 1):
if num1 % i == 0 and num2 % i == 0:
gong_yue_shu = i
#4.最小公倍数
gong_bei_shu=int((num1 * num2) / gong_yue_shu)
print('%s和%s的最大公约数为:%s' %(num1,num2,gong_yue_shu))
print('%s和%s的最大公倍数为:%s' %(num1,num2,gong_bei_shu))
==------->测试结果如下:==