当前位置: 首页 > 工具软件 > Whiley > 使用案例 >

python用while循环求n!_python while循环语句以及练习题

邓建柏
2023-12-01

9. 循环

死循环

import time

while 1 == 1:

print('ok',time,time())

not死循环

import time

count = 0

while count < 10:

print(count)

count == count + 1

print(123)

补continue、break:

count = 0

while count < 10:

if count == 7:

count = count +1

continue # 不再向下执行,直接回到while继续进行循环

print(count)

count = count + 1

count = 0

while count < 10:

if count == 7:

break # 终止所有循环

print(count)

count = count + 1

练习题

if条件语句

while循环

奇数偶数

1、使用while循环输入1 2 3 4 5 6 8 9 10

n = 1

while n < 11:

if n == 7:

pass

else:

print(n)

n = n + 1

print('end')

2、求1 - 100的所有数的和

n = 1

s = 0

while n < 101:

s = s + n

n = n + 1

print(s)

3、输出1 - 100内所有的奇数

n = 1

while n < 101:

temp = n % 2

if temp == 0:

pass

else:

print(n)

n = n + 1

print('end')

4、输出1 - 100内所有的偶数

n = 1

while n < 101:

temp = n % 2

if temp == 0:

print(n)

else:

pass

n = n + 1

print('end')

5、求1-2+3-4+5...99的所有数的和

n = 1

s = 0 # s是之前所有数的和

while n < 100:

temp = n % 2

if temp == 0:

s = s - n

else:

s = s + n

n = n + 1

print(s)

6、用户登录(三次重试的机会)

n = 0

while n < 3:

inp == input('请输入密码:')

if inp == woaini:

print('登陆成功')

break

else:

n = n + 1

print('登录失败')

 类似资料: