• while练习


    # 作业(必做题):
    #1. 使用while循环输出1 2 3 4 5 6 8 9 10
    cout = 1
    while cout < 10:
        cout += 1
        if cout == 7:
            continue
        print(cout,end=" ")
    print("")


    #2. 求1-100的所有数的和
    cout = 0
    c_sum = 0
    while cout < 100:
        cout += 1
        c_sum += cout
    print(c_sum)

    #3. 输出 1-100 内的所有奇数
    cout = 1
    while cout <= 100:
        if cout % 2 == 1:
            print(cout,end=" ")
        cout += 1
    print("")
    #4. 输出 1-100 内的所有偶数
    cout = 1
    while cout <= 100:
        if cout % 2 == 0:
            print(cout,end=" ")
        cout += 1
    print("")
    #5. 求1-2+3-4+5 ... 99的所有数的和
    cout = 1
    c_sum = 0
    while cout < 100:
        if cout % 2 == 0:
            c_sum -= cout
        else:
            c_sum += cout
        cout += 1
    print(c_sum)

    #6. 用户登陆(三次机会重试)
    name_data = {"Jil":1118, "Fishball":188741}
    cout = 0
    while cout < 3:
        name = input("Please input your username:")
        psw = input("Please input your password:")
    
        if name in name_data and psw == name_data.get(name):
            print("Congratulation!Log in!")
            break
        else:
            print("Account number or password error, login failed!")
            cout += 1
    else:
        print("3 times error!bang!")


    #7:猜年龄游戏
    # 要求:
    # 允许用户最多尝试3次,3次都没猜对的话,就直接退出,如果猜对了,打印恭喜信息并退出
    cout = 0
    s_age = 15
    while cout < 3:
        age = input("Please input your guessing age:")
        if age == s_age:
            print("Bingo!")
            break
        else:
            print("Wrong number!")
            cout += 1

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

    import random
    guess_age = random.randint(0,150)
    cout = 0
    tag = True
    while tag:
        while cout < 3:
            age = input("Please input your guessing number:")
            if age.isdigit():
                if int(age) < guess_age:
                    print("think bigger!")
                    cout += 1
                elif int(age) > guess_age:
                    print("think smaller!")
                    cout += 1
                else:
                    print("Bingo!")
                    Tag = False
                    break
            else:
                print("Please input integer!")
        else:
            flag = input("Continue?(Y/N)")
            if flag =="Y" or flag == "y":
                cout = 0
            elif flag == 'N' or flag == 'n':
                tag = False                           #
            else:
                print("Wrong input!Try again!")
  • 相关阅读:
    android 68 单元测试
    android 67 生成和解析xml
    android 66 sharedperference的使用
    android 65 文件访问权限
    android 64 sd卡读写的操作
    EditText操作收集
    android Fragments详解六:处理fragement的生命周期
    Context
    android 63 Fragment
    在Centos安装oracle_11gR2进度68%"Error in invoking target mkldflags ntcontab.o nnfgt.o of makefile..”
  • 原文地址:https://www.cnblogs.com/zhubincheng/p/12448804.html
Copyright © 2020-2023  润新知