zoukankan      html  css  js  c++  java
  • 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!")
  • 相关阅读:
    Linux下sed,awk,grep,cut,find学习笔记
    Python文件处理(1)
    KMP详解
    Java引用详解
    解决安卓中页脚被输入法顶起的问题
    解决swfupload上传控件文件名中文乱码问题 三种方法 flash及最新版本11.8.800.168
    null id in entry (don't flush the Session after an exception occurs)
    HQL中的Like查询需要注意的地方
    spring mvc controller间跳转 重定向 传参
    node to traverse cannot be null!
  • 原文地址:https://www.cnblogs.com/zhubincheng/p/12448804.html
Copyright © 2011-2022 走看看