1. 使用while循环输出1 2 3 4 5 6 8 9 10
i =1
while i <= 10:
print(i,end=' ')
i = i +1
if i == 7:
i = i + 1
print(" ",end='')
continue
2. 求1-100的所有数的和
sum = 0
for i in range(101):
sum = sum + i
print(sum)
3. 输出 1-100 内的所有奇数
for i in range(101):
if i % 2 == 1:
print(i,end=' ')
4. 输出 1-100 内的所有偶数
for i in range(1,101):
if i % 2 == 0:
print(i,end=' ')
5. 求1-2+3-4+5 ... 99的所有数的和
sum = 0
for i in range(100):
if i % 2 == 1:
sum = sum + i
if i % 2 == 0:
sum =sum -i
print(sum)
6. 用户登陆(三次机会重试)
name = 'insnce'
for i in range(3):
s = input('请输入用户名')
if s == name:
print('登录成功')
break
else:
print('登录失败')
continue
7:猜年龄游戏
要求:
允许用户最多尝试3次,3次都没猜对的话,就直接退出,如果猜对了,打印恭喜信息并退出
age = 18
for i in range(3):
s = int(input('请输入要猜的年龄'))
if s == age:
print('恭喜你猜对了')
break
else:
print('猜错了')
continue
8:猜年龄游戏升级版(选做)
要求:
允许用户最多尝试3次
每尝试3次后,如果还没猜对,就问用户是否还想继续玩,如果回答Y或y, 就继续让其猜3次,以此往复,如果回答N或n,就退出程序
如何猜对了,就直接退出
while True:
for i in range(3):
s = int(input('请输入要猜的年龄'))
if s == age:
print('恭喜你猜对了')
break
else:
print('猜错了')
count = count + 1
continue
if s == age:
break
if count%3 == 0:
answer = input('是否继续猜,是的话回复y或Y,否的话回复n或N:')
if answer == 'n' or answer'N':
break
elif answer == 'y' or answer'Y':
continue
9.for循环打印99乘法表
for i in range(1,10):
for j in range(1,i+1):
print(f'{i}x{j}={i*j}',end=' ')
print('')
10.for循环打印金字塔:如下
for i in range(1,10,2):
print(f'{""i:^10}',end='
')