一、必做题
1、使用while循环输出1 2 3 4 5 6 8 9 10
print('{:-^40}'.format('使用while循环输出1 2 3 4 5 6 8 9 10'))
number = 1
while number < 11: # number条件执行范围:1~10
if number == 7:
number += 1
continue
print(number, end=" ")
number += 1
else:
print()
2、求1-100的所有数的和
init_number = 1
sum_number = 0
while init_number < 101: # init_number条件执行范围:1~100
sum_number += init_number
init_number += 1
print('sum_number:', sum_number) # sum_number: 5050
3、输出 1-100 内的所有奇数
print('{:-^40}'.format('输出 1-100 内的所有奇数'))
odd_number = 1
while odd_number < 100: # odd_number条件执行范围:1~99
if odd_number % 2: # 奇数除以2,都是余1
print(odd_number, end=' ')
odd_number += 1
else:
print()
4、输出 1-100 内的所有偶数
print('{:-^40}'.format('输出 1-100 内的所有偶数'))
even_number = 1
while even_number < 100: # even_number条件执行范围:1~99
if not even_number % 2: # 偶数除以2,都是余0
print(even_number, end=" ")
even_number += 1
5、求1-2+3-4+5 ... 99的所有数的和
number_of_poor = 1
sum_of_poor = 0
while number_of_poor < 100: # number_of_poor条件执行范围:1~99
if number_of_poor % 2: # 奇数除以2,都是余1
sum_of_poor += number_of_poor
else:
sum_of_poor -= number_of_poor
number_of_poor += 1
else:
print('sum_of_poor:', sum_of_poor) # sum_of_poor: 50
6、用户登陆(三次机会重试)
username = 'egon'
password = '123'
wrong_number = 0
while wrong_number < 3: # wrong_number条件执行范围:1~2,错误3次,循环退出
inp_username = input('please input your username>>:').strip()
inp_password = input('please input your password>>:').strip()
if inp_username == username and inp_password == password:
print('登录成功')
break
else:
print('[{name}] 登录失败 [{wrong_num}]次'.format(name=username, wrong_num=wrong_number+1))
wrong_number += 1
else:
print('登录失败三次啦,要退出咯!')
7、猜年龄游戏
- 要求:允许用户最多尝试3次,3次都没猜对的话,就直接退出,如果猜对了,打印恭喜信息并退出
age = 18
wrong_guess_number = 0
while wrong_guess_number < 3: # wrong_guess_number条件执行范围:1~2,错误3次,循环退出
guess_age = input('猜猜我的年龄>>:').strip()
if guess_age.isdigit():
guess_age = int(guess_age)
if guess_age == age:
print("恭喜!恭喜!猜对了!!!")
break
else:
print('猜错[{}]次'.format(wrong_guess_number + 1))
wrong_guess_number += 1
else:
print('输入无效。。。')
else:
print('尝试3次,3次都没猜对,退出!!!')
二、选做题: 猜年龄游戏升级版(选做题)
- 要求:允许用户最多尝试3次每尝试3次后,如果还没猜对,就问用户是否还想继续玩,如果回答Y或y, 就继续让其猜3次,以此往复,如果回答N或n就退出程序,如果猜对了,就直接退出
- 方法一
age1 = 19
wrong_guess_number1 = 0
while wrong_guess_number1 < 3: # wrong_guess_number条件执行范围:1~2,错误3次,循环退出
guess_age = input('猜猜我的年龄>>:').strip()
if guess_age.isdigit():
guess_age = int(guess_age)
if guess_age == age1:
print("恭喜!恭喜!猜对了!!!")
break
else:
print('猜错[{}]次'.format(wrong_guess_number1 + 1))
wrong_guess_number1 += 1
# 用户最多尝试3次每尝试3次后,如果还没猜对,就问用户是否还继续玩
if wrong_guess_number1 == 3:
while True:
con = input("是否还继续玩,回答Y或y, 就继续.回答N或n,就退出程序:").strip()
if con.lower() == 'y':
wrong_guess_number1 = 0
print("你继续玩吧!")
break
elif con.lower() == 'n':
exit()
else:
print("输入错误。。。。")
else:
print('输入无效。。。')
else:
print('尝试3次,3次都没猜对,退出!!!')
age1 = 19
wrong_guess_number1 = 0
tag = True
while tag: # wrong_guess_number条件执行范围:1~2,错误3次,循环退出
# 用户最多尝试3次每尝试3次后,如果还没猜对,就问用户是否还继续玩
if wrong_guess_number1 == 3:
while True:
con = input("是否还继续玩,回答Y或y, 就继续.回答N或n,就退出程序:").strip()
if con.lower() == 'y':
wrong_guess_number1 = 0
print("你继续玩吧!")
break
elif con.lower() == 'n':
exit()
else:
print("输入错误。。。。")
guess_age = input('猜猜我的年龄>>:').strip()
if guess_age.isdigit():
guess_age = int(guess_age)
if guess_age == age1:
print("恭喜!恭喜!猜对了!!!")
tag = False
else:
print('猜错[{}]次'.format(wrong_guess_number1 + 1))
wrong_guess_number1 += 1
else:
print('输入无效。。。')
else:
print('尝试3次,3次都没猜对,退出!!!')