作业(必做题):
#1. 使用while循环输出1 2 3 4 5 6 8 9 10

num = 1
while num <= 10:
    if num != 7:
        print(num, end=" ")
    else:
        print("     ", end="")
    num += 1
else:
    print("")

 


#2. 求1-100的所有数的和

sum1 = 0
num = 1
while num <= 100:
    sum1 += num
    num += 1
print(sum1)

 


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

num = 1
while num <= 100:
    if num % 2 == 1:
        print(num)
    num += 1

 


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

num = 1
while num <= 100:
    if num % 2 == 0:
        print(num)
    num += 1

 


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

sum1 = 0
num = 1
while num < 100:
if num % 2 == 1:
sum1 += num
else:
sum1 -= num
num += 1
print(sum1)


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

count = 0
while count <= 3:
    user = input("请输入用户名:")
    password = input("请输入密码:")
    if user == "wu" and password == "123":
        print("登陆成功")
        break
    else:
        count += 1
        print('登录失败,请重新输入')

 


#7:猜年龄游戏
要求:
允许用户最多尝试3次,3次都没猜对的话,就直接退出,如果猜对了,打印恭喜信息并退出

 

count = 0
age = "22"
while count <= 2:
    gress = input("请输入你猜的年龄:")
    if age == gress:
        print("恭喜你猜对了!")
        break
    else:
        count += 1
        if count <= 2:
            print('猜错啦,请重新猜')
else:
    print("你失败了")

 

 

 

#8:猜年龄游戏升级版(选做题)
要求:
允许用户最多尝试3次
每尝试3次后,如果还没猜对,就问用户是否还想继续玩,如果回答Y或y, 就继续让其猜3次,以此往复,如果回答N或n,就退出程序
如何猜对了,就直接退出

 

count = 0
age = "22"
conti = 1
while conti:
    while count <= 2:
        gress = input("请输入你猜的年龄:")
        if age == gress:
            print("恭喜你猜对了!")
            conti = 0
            break
        else:
            count += 1
            if count <= 2:
                print('猜错啦,请重新猜')
    else:
        result = input("三次机会用完了!请问选择是否重新玩一次:(y或Y继续,n或N退出)")
        if result.upper() == "N":
            conti = 0
        elif result.upper() == "Y":
            conti = 1
            count = 0

 

 posted on 2020-03-09 15:37  wwwpy  阅读(164)  评论(0编辑  收藏  举报