作业20200309

1 使用while循环输出1 2 3 4 5 6 8 9 10

count = 1
while count <= 10:
    if count == 7:
        count += 1
        continue
    print(count, end=' ')
    count += 1

# output: 1 2 3 4 5 6 8 9 10 

2 求1-100的所有数的和

count = 1
total_sum = 0
while count <= 100:
    total_sum += count
    count += 1
print(total_sum)

# output: 5050

3 输出 1-100 内的所有奇数

count = 0
col = 10
while count < 100:
    count += 1
    if count % 2 == 1:
        print(count, end='\t')
    if count % col == 0:
        print()

# output:
1	3	5	7	9	
11	13	15	17	19	
21	23	25	27	29	
31	33	35	37	39	
41	43	45	47	49	
51	53	55	57	59	
61	63	65	67	69	
71	73	75	77	79	
81	83	85	87	89	
91	93	95	97	99

4 输出 1-100 内的所有偶数

count = 0
col = 10
while count <= 100:
    count += 1
    if count % 2 == 0:
        print(count, end='\t')
    if count % col == 0:
        print()
        
# output:
2	4	6	8	10	
12	14	16	18	20	
22	24	26	28	30	
32	34	36	38	40	
42	44	46	48	50	
52	54	56	58	60	
62	64	66	68	70	
72	74	76	78	80	
82	84	86	88	90	
92	94	96	98	100	

5 求1-2+3-4+5 ... 99的所有数的和

# 基础版
count = 1
total_sum = 0
while count < 100:
    tmp = count
    if tmp % 2 == 0:
        tmp *= -1
    total_sum += tmp
    count += 1
print(total_sum)

# 改进版
count = 1
total_sum = 0
while count < 100:
    flag = 1
    if count % 2 == 0:
        flag = -1
    total_sum += flag*count
    count += 1
print(total_sum)

# output: 50

6 用户登陆(三次机会重试)

name = 'egon'
pawd = '1234'

count = 0
while count < 3:

    username = input('username >>>:').strip()
    password = input('password >>>:').strip()

    if username == name and password == pawd:
        print('登录成功!')
        # 登录成功后执行相关操作
        while True:
            cmd = input('请输入指令(退出请输入:q) >>>').strip().lower()
            if cmd == 'q':
                break
            print(f'正在执行的操作是:{cmd}')
        break

    else:
        count += 1
        if count != 3:
            print(f'用户名或密码错误!您还有:{3-count} 次登录机会')

else:
    print('登录次数超过三次,强制退出')

7 猜年龄游戏

  • 要求:允许用户最多尝试3次,3次都没猜对的话,就直接退出,如果猜对了,打印恭喜信息并退出
std_age = 18
count = 0
while count < 3:
    age = input('请输入年龄:').strip()
    if age.isdigit():
        age = int(age)
        if age == std_age:
            print('恭喜,猜对了')
            break
        elif age < std_age: print('猜小了')
        else: print('猜大了')
            
        count += 1
    else:
        print('请输入合法的年龄')

8 猜年龄游戏升级版(选做题)

  • 允许用户最多尝试3次
  • 每尝试3次后,如果还没猜对,就问用户是否还想继续玩,如果回答Y或y, 就继续让其猜3次,以此往复,如果回答N或n,就退出程序;如何猜对了,就直接退出
std_age = 18
count = 0
while count < 3:
    age = input('请输入年龄:').strip()
    if age.isdigit():
        age = int(age)
        if age == std_age:
            print('恭喜,猜对了')
            break
        elif age < std_age: print('猜小了')
        else: print('猜大了')

        count += 1
        #使用了三次机会
        if count == 3:
            while True:
                choice = input('是否继续(Y or N) >>>: ').strip().lower()
                if choice == 'y':
                    count = 0
                    break
                elif choice == 'n': break
                else: print('输入错误,请重新确定')

    else: print('请输入合法的年龄')
posted @ 2020-03-09 15:57  the3times  阅读(21)  评论(0)    收藏  举报