day07作业:
1. 使用while循环输出1 2 3 4 5 6 8 9 10 #!/usr/bin/env python #-*- coding:utf-8 -*- def main(): count = 0 while count < 10: count += 1 if count == 7: continue else: print(count) if __name__ == '__main__': main() 2、求1-100的所有数的和 #!/usr/bin/env python #-*- coding:utf-8 -*- def main(): summ = 0 for n in range(1,101): summ += n print('1到100所有数的和为:{}'.format(summ)) if __name__ == '__main__': main() 3、输出 1-100 内的所有奇数 #!/usr/bin/env python #-*- coding:utf-8 -*- def main(): list_odd = [obj for obj in range(1,101) if obj%2 ==1] print('1到100的所有奇数为:{}'.format(list_odd)) if __name__ == '__main__': main() 4、 输出 1-100 内的所有偶数 #!/usr/bin/env python #-*- coding:utf-8 -*- def main(): list_even = [obj for obj in range(1,101) if obj%2 == 0] print('1到100所有的偶数为:{}'.format(list_even)) if __name__ == '__main__': main() 5、求1-2+3-4+5 ... 99的所有数的和 #!/usr/bin/env python #-*- coding:utf-8 -*- def main(): summ = 0 list_1 = [i for i in range(1,100) if i%2 == 1]+[-j for j in range(1,100) if j%2 == 0] for obj in list_1: summ += obj print('1-2+3-4+5...99的所有数的和为:{}'.format(summ)) if __name__ == '__main__': main() 6、用户登陆(三次机会重试) #!/usr/bin/env python #-*- coding:utf-8 -*- import sys USER_INFO = {'egon':'vip666', 'alex':'super888', 'tank':'hopec66'} def main(): count = 0 while 1: name_of_user = input('请输入你的账号:').strip() passwd_of_user = input('请输入你的密码:').strip() if name_of_user in USER_INFO.keys(): if passwd_of_user == USER_INFO.get(name_of_user): print('登录成功!') sys.exit(0) else: print('密码错误,请重新输入!') count += 1 else: print('账号不存在,请重新输入!') count += 1 if count == 3: print('账号输错3次,退出系统!') sys.exit(0) if __name__ == '__main__': main() 7/8、要求: 允许用户最多尝试3次 每尝试3次后,如果还没猜对,就问用户是否还想继续玩,如果回答Y或y, 就继续让其猜3次,以此往复,如果回答N或n,就退出程序 如何猜对了,就直接退出 #!/usr/bin/env python #-*- coding:utf-8 -*- import sys import time def main(): age_of_egon = 18 count = 0 while count<3 : guess_age = int(input('情输入你猜测的年龄:').strip()) if guess_age == age_of_egon: print('猜对了,退出系统!') sys.exit(0) else: print('猜错了,请重新输入!') count += 1 if count == 3: if_continue = input('输错3次,是否继续!按Y(y)继续,按N(n)退出:').strip() if if_continue.upper() == 'Y': count = 0 elif if_continue.upper() == 'N': continue else: print('输入错误,默认退出!') sys.exit(0) else: tm_list = time.ctime(time.time()).split(' ')[-1:-3:-1] current_time = tm_list[0]+' '+tm_list[1] print('{},退出系统!'.format(current_time)) if __name__ == '__main__': main()