Python学习0309作业
# @Author:lsj # @version V1.0 # -*- coding:UTF-8 -*- # 作业(必做题): # # 1. 使用while循环输出1 2 3 4 5 6 8 9 10 # count = 1 # while count <= 10: # if count != 7: # # print("使用while循环输出:{x}".format(x=count)) # print(count) # else: # print() # count += 1 # #2. 求1-100的所有数的和 # num = 1 # count = 0 # while num <= 100: # count += num # num += 1 # print("2. 求1-100的所有数的和:%d" % count) # #3. 输出 1-100 内的所有奇数 # num = 1 # while num <= 100: # if num % 2 == 1: # print("3. 输出 1-100 内的所有奇数:{n}".format(n = num)) # num += 1 # #4. 输出 1-100 内的所有偶数 # num = 1 # while num <= 100: # if num % 2 == 0: # res = f'4. 输出 1-100 内的所有偶数:{num}' # print(res) # num += 1 # #5. 求1-2+3-4+5 ... 99的所有数的和 # num = 1 # count = 0 # while num <= 99: # if num % 2 == 1: # count += num # elif num % 2 == 0: # # count += (-num) # count -= num # num += 1 # print("#5. 求1-2+3-4+5 ... 99的所有数的和:%d" % count) # #6. 用户登陆(三次机会重试) # user = ["lsj",123] # count = 0 # while count <= 3: # if count == 3: # print("请重试!!!") # count = 0 # username = input("请输入您的账号:") # password = int(input("请输入您的密码:")) # if username == user[0] and password == user[1]: # print("登录成功!!") # break # else: # print("登录失败!!!") # count += 1 """ #7:猜年龄游戏 要求: 允许用户最多尝试3次,3次都没猜对的话,就直接退出,如果猜对了,打印恭喜信息并退出 """ # age = 18 # count = 0 # while count <= 3: # if count == 3: # print("没有猜中!") # break # input_age= int(input("请输出您要猜的年龄:")) # if input_age == age: # print("恭喜您猜对了!!!") # break # count += 1 """ #8:猜年龄游戏升级版(选做题) 要求: 允许用户最多尝试3次 每尝试3次后,如果还没猜对,就问用户是否还想继续玩,如果回答Y或y, 就继续让其猜3次,以此往复,如果回答N或n,就退出程序 如何猜对了,就直接退出 """ # age = 18 # count = 0 # while count <= 3: # if count == 3: # answer = input("您是否还要玩?回答Y或y是继续玩,回答N或n可退出!!!") # if answer == 'Y' or answer == 'y': # count = 0 # elif answer == 'N' or answer == 'n': # count = 4 # break # input_age = int(input("请输入您猜的年龄:")) # if input_age == age: # print("恭喜你猜对了!!") # break # count += 1 # print("欢迎您下次再来")