07----注意注意!!!我是作业
1、使用while循环输出1 2 3 4 5 6 8 9 10
count = 0 while count < 10: count += 1 if count == 7: continue print(count)
2、求1-100的所有数的和
i = 1 res = 0 while i <= 100: res += i i += 1 print(res)
3、输出 1-100 内的所有奇数
i = 1 while i <= 100: if i % 2 == 1: print(i) i += 1
4、输出 1-100 内的所有偶数
i = 1 while i <= 100: if i % 2 == 0: print(i) i += 1
5、求1-2+3-4+5 ... 99的所有数的和
i = 1 res = 0 while i < 100: if i % 2 == 0: res -= i else: res += i i += 1 print(res)
6、用户登陆(三次机会重试)
user = 'egon' password = '1234' # 计算输错次数 count = 0 while count < 3: username = input('your name:') user_password = input('your password:') if username == user and user_password == password: while 1: cmd = input('please your cmd:') if cmd == 'q': print('quit!') break else: print('{cmd} is loading...'.format(cmd = cmd)) break else: print('invalid username or password! *{count} times'.format(count=count+1)) count += 1 else: print('这是你的银行卡嘛!!!臭不要脸!')
7、猜年龄游戏
要求:允许用户最多尝试3次,3次都没猜对的话,就直接退出,如果猜对了,打印恭喜信息并退出
count = 0 age_egon = 80 while count < 3: age = int(input('age:')) if age == age_egon: print('get it!') break else: print('you are wrong! please try again,remain {count} times'.format(count = 2-count)) count += 1
8、猜年龄游戏升级版(选做题)
要求:
允许用户最多尝试3次
每尝试3次后,如果还没猜对,就问用户是否还想继续玩,如果回答Y或y, 就继续让其猜3次,以此往复,如果回答N或n,就退出程序
如何猜对了,就直接退出
count = 0 age_egon = 80 while count < 3: age = input('age:') # 判断输入的是否是数字 if age.isdigit(): age = int(age) # 判断用户输入是否正确 if age == age_egon: print('great !') break else: # 判断猜大了还是猜小了。 if age > age_egon: print('猜大了') else: print('猜小了') count += 1 if count == 3: choice = input('是否继续?y or Y==继续,n or N==退出:') if choice == 'N' or choice == 'n': print('劳资就是不想玩了') break elif choice == 'Y' or choice == 'y': print('继续继续') count = 0 else: print('请输入数字!最好100以内,因为egon可能还没那么老。。。')