zoukankan      html  css  js  c++  java
  • 007作业

    作业(必做题):

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

    count=1
     while count < 11:
         if count == 7:
             count+=1
             continue
         print(count)
         count+=1
    

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

    res=0
    count=1
    while count < 101:
        res=res+count
        count+=1
    print(res)
    

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

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

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

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

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

    res=0
    count=1
    while count < 100:
        if count%2==1:
            res+=count
        if count%2==0:
            res-=count
        count+=1
    print(count)
    

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

    name='egon'
    pwd='123'
    count=1
    while count < 4:
        inp_name=input('请输入用户名:')
        inp_pwd=input('请输入密码:')
        if inp_name == name and inp_pwd == pwd:
            print('登录成功')
            break
        else:
            print('用户名或密码错误')
            count+=1
    

    7:猜年龄游戏

    要求:

    允许用户最多尝试3次,3次都没猜对的话,就直接退出,如果猜对了,打印恭喜信息并退出

    age=18
    count=1
    while count < 4:
        guess_age=int(input('猜多大?'))
        if guess_age == age:
            print('猜对了')
            break
        else:
            print('猜错了')
            count+=1
    

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

    要求:

    允许用户最多尝试3次

    每尝试3次后,如果还没猜对,就问用户是否还想继续玩,如果回答Y或y, 就继续让其猜3次,以此往复,如果回答N或n,就退出程序

    如何猜对了,就直接退出

    age=18
    count=1
    while 1:
        if count == 4:
            choice=input('继续玩吗?y/n:')
            if choice == 'y' or choice == 'Y':
                count=1
            else:
                break
        guess_age=int(input('猜多大?'))
        if guess_age == age:
            print('猜对了')
            break
        else:
            print('猜错了')
            count+=1
    作者:Joab
    本文版权归作者和博客园所有,欢迎转载,转载请标明出处。
    如果您觉得本篇博文对您有所收获,请点击右下角的 [推荐],谢谢!
  • 相关阅读:
    re.match() and re.search()
    libxml2 使用教程【转】
    Spring PropertyPlaceholderConfigurer类载入外部配置
    Spring PropertyPlaceholderConfigurer类载入外部配置
    Spring PropertyPlaceholderConfigurer类载入外部配置
    Spring PropertyPlaceholderConfigurer类载入外部配置
    前缀和、前缀积
    前缀和、前缀积
    前缀和、前缀积
    前缀和、前缀积
  • 原文地址:https://www.cnblogs.com/linqiaobao/p/12450458.html
Copyright © 2011-2022 走看看