7.17 作业
1. 使用while循环输出1 2 3 4 5 6 8 9 10
i = 0
while i<10:
i+=1
if i == 7:
continue
print(i,end='')
2. 求1-100的所有数的和
s = 0
t = 0
while s<100:
s+=1
t+=s
print(t)
3. 输出 1-100 内的所有奇数
s = 0
while s<100:
s+=1
if s % 2 == 1:
print(s,end=' ')
4. 输出 1-100 内的所有偶数
s = 0
while s<100:
s+=1
if s % 2 == 0:
print(s,end=' ')
5. 求1-2+3-4+5 ... 99的所有数的和
s = 0
t = 0
while s<99:
s+=1
if s%2==1:
t+=s
if s%2==0:
t-=s
print(t)
6. 用户登陆(三次机会重试)
id1 = 'wzh'
pwd1 = '123'
c = 1
while c<4:
c+=1
id = input('please enter id:')
password = input('please enter password:')
if id == id1 and password == pwd1:
print('wellcom!')
break
7:猜年龄游戏
要求: 允许用户最多尝试3次,3次都没猜对的话,就直接退出,如果猜对了,打印恭喜信息并退出
right = 18
for i in range(3):
i += 1
s = input('please enter the age as your mean:')
if int(s) == right:
print("your enter's age is right !!!")
break
8:猜年龄游戏升级版(选做)
要求: 允许用户最多尝试3次 每尝试3次后,如果还没猜对,就问用户是否还想继续玩,如果回答Y或y, 就继续让其猜3次,以此往复,如果回答N或n,就退出程序 如何猜对了,就直接退出
right = 18
while True:
t = input('do you want to play the game? please enter y/Y or n/N:')
if t == 'y' or t == 'Y':
# 更改打印字体颜色,后面一样
print('\033[1;35;0m game beginning!\033[0m')
for i in range(3):
i += 1
s = input('please enter the age as your mean:')
if int(s) == right:
print('\033[1;35;0m your enter\'s name is right !!! \033[0m')
break
else:
print('\033[1;35;0m your enter\'s age is wrong\033[0m')
else:
print('\033[1;35;0m game over!\033[0m')
break
9.for循环打印99乘法表
for i in range(1,10):
print(end="\n")
for j in range(1,i+1):
print(str(i)+'*'+str(j)+'='+str(i*j),end=' ')
10.for循环打印金字塔:
s = input('please enter the pyramid\'s length(1-50):')
for i in range(int(s)):
print(f'{"*"*(2*i+1): ^100}')