python中for循环和while循环的区别_python中的程序中断、for循环和while循环

1.程序中断

break:跳出整个循环,不会再执行循环后面的内容

continue:跳出本次循环,continue后面的代码不再执行,但是循环依然继续

exit():结束程序的运行

for i in range(10):

if i == 5:

# continue

# break

exit()

print('hello')

print(i)

print('hello python')

f83e120aa6c2ff6db970f0f6c57d0105

507225c80b231a812052ee18774e36fd

04c7cedf20732befa35d73867c79fed4

for循环

for循环语法:

for 变量 in range(x):

循环需要执行的代码

range(stop): 0 ~ stop-1

range(start,stop): start ~ stop-1

range(start,stop,step): start ~ stop-1 step(步长)

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))

966c0f0e620b7f6c4e2ce804f0c9040a

2.求1~100之间的所有偶数求和。

sum = 0

for i in range(2,101,2):

sum += i

print(sum)

dc5dfd4c1ce911877b976c4e7cd94a16

3.有1,2,3,4四个数字。求四个数字能生成多少个互不相同且无重复数字的三位数(不能含有类似122 133)。

count = 0

for i in range(1,5):

for j in range(1,5):

for k in range(1,5):

if i != j and j !=k and k != i:

print(i * 100 + j * 10 + k)

count += 1

print('生成%d个无重复数字的三位数' %count)

674fcad71dd77dfbc36d27f9923c7a74

4.用户登陆系统,要求:输入用户名和密码;判断是否正确(name = ‘root’,passwd = ‘westos’);登录仅有3次及会,超过3次,报错。

for i in range(3):

name = input('用户名: ')

passwd = input('密码: ')

if name == 'root' and passwd == 'westos':

print('登录成功!')

break

else:

print('登录失败')

print('您还剩余%d次机会' %(2-i))

else:

print('失败超过3次,请稍后再试!')

6b033585f769b7a63bf6d044411c0a30

5.给定两个数,求这两个数的最小公倍数和最大公约数。

#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:

gys = i

#4.最小公倍数

gbs = (num1*num2 / gys)

print('%d和%d的最大公约数为: %d' %(num1,num2,gys))

print('%d和%d的最小公倍数为: %d' %(num1,num2,gbs))

f85019e4e108df1e5bded37be8da6fee

3.while循环

while 条件:

条件满足时,做的事情1

条件满足时,做的事情2

#1.定义计数器

i = 0

#2.开始循环

while i < 3:

#循环内需要做的事情

print('hello python')

#处理计数器

i += 1

d9f960ee137bd8b38c2d44ee51c99a7f

一直循环

while True:

print('hello python')

b67ad3d696fae47a1088d69499c8c41b

练习:

1.求1~100的和

i = 0

sum = 0

while i <= 100:

sum += i

i += 1

print('0~100之间的数字求和结果为: %d' %sum)

181cfde6fac4265bc823af2a3b92a2e3

2.用户登陆三次失败提示。

trycount = 0

while trycount < 3:

name = input('用户名: ')

passwd = input('密码: ')

if name == 'root' and passwd == 'westos':

print('登录成功!')

break

else:

print('登录失败')

print('您还剩余%d次机会' %(2-trycount))

trycount += 1

else:

print('失败超过3次,请稍后再试!')

f752c1ddefd6afce2343c2a6fc98e3df

b5291a4c12960e54b88ea7607854f1e0

3.猜数字

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('恭喜')

break

99f19de567184cc5bea17e4d25e6c6ba

9fbfa2172eda4dcbbc894295ad06531f

4.在控制台连续输出五行*,每行依次递增,即使用while循环显示出以下图形:

*****

****

***

**

*

row = 5

while row >= 1:

col = 1

while col <= row:

print('*',end='')

col += 1

print('')

row -= 1

0c27ee93bcdea72a1d1781695bd2d45f

*

**

***

****

*****

row = 1

while row <= 5:

col = 1

while col <= row:

print('*',end='')

col += 1

print('')

row += 1

p><p>213ff

5.在python的IDE中输出类似于shell界面中的内容并且可以成功使用命令。

import os

for i in range(1000):

cmd = input('[kiosk@python ~]$ ')

if cmd:

if cmd == 'exit':

print('logout')

else:

os.system(cmd)

else:

continue

29223ca410cc50b636411d0e072bfe32

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值