• 老男孩python学习第二天作业


    问题一:猜数字,设定一个理想数字比如:66,让用户输入数字,如果比66大,则显示猜测的结果大了;如果比66小,则显示猜测的结果小了;只有等于66,显示猜测结果正确,然后退出循环。

    答:

    #!/user/bin/env python
    # -*- coding:utf-8 -*-
    number = input('请输入猜测的数字:')
    number = int(number)
    if number > 66:
        print('大了')
    elif number < 66:
        print('小了')
    else:
        print('猜对了')
    

    问题二:在上一题的基础,设置:给用户三次猜测机会,如果三次之内猜测对了,则显示猜测正确,退出循环,如果三次之内没有猜测正确,则自动退出循环,并显示‘大笨蛋’。

    答:

    #!/user/bin/env python
    # -*- coding:utf-8 -*-
    count = 1
    while count <= 3:
        number = input('请输入猜测的数字:')
        number = int(number)
        if number > 66:
            print('大了')
        elif number < 66:
            print('小了')
        else:
            print('猜对了')
            break
        count += 1
    else:
        print('大笨蛋')
    

    问题三:使用两种方法实现输出 1 2 3 4 5 6 8 9 10 。

    答:

    方法一:

    #!/user/bin/env python
    # -*- coding:utf-8 -*-
    number = 1
    while number <= 10:
        if number == 7:
            number += 1
            continue
        else:
            print(number)
        number += 1
    

    方法二:

    #!/user/bin/env python
    # -*- coding:utf-8 -*-
    number = 1
    while number <= 10:
        if number == 7:
            pass
        else:
            print(number)
        number += 1
    

    问题四:求1-100的所有数的和

    答:

    #!/user/bin/env python
    # -*- coding:utf-8 -*-
    total = 0
    number = 1
    while number <= 100:
        total += number
        number += 1
    print(total)
    

    问题五:输出 1-100 内的所有奇数

    答:

    #!/user/bin/env python
    # -*- coding:utf-8 -*-
    number = 1
    while number <= 100:
        if number % 2 == 1:
            print(number)
        number += 1
    

    问题六:输出 1-100 内的所有偶数

    答:

    #!/user/bin/env python
    # -*- coding:utf-8 -*-
    number = 1
    while number <= 100:
        if number % 2 == 0:
            print(number)
        number += 1
    

    问题七:使用while 循环输出100-50,从大到小,如100,99,98…,到50时再从0循环输出到50,然后结束

    #!/user/bin/env python
    # -*- coding:utf-8 -*-
    number = 100
    while True:
        if number >= 50:
            print(number)
            number -= 1
            if number == 50:
                print(number)
                number = 0
        else:
            print(number)
            number += 1
            if number == 50:
                print(number)
                break
    

    问题八:求1-2+3-4+5 … 99的所有数的和

    答:

    #!/user/bin/env python
    # -*- coding:utf-8 -*-
    total = 0
    number = 1
    while number < 100:
        if number % 2 == 1:
            total += number
        else:
            total -= number
        number += 1
    print(total)
    

    问题九:⽤户登陆(三次输错机会)且每次输错误时显示剩余错误次数(提示:使⽤字符串格式化)

    答:

    #!/user/bin/env python
    # -*- coding:utf-8 -*-
    count = 2
    while count >= 0:
        name = input('请输入用户名:')
        pwd = input('请输入密码:')
        if name == 'alex' and pwd == '123':
            print('欢迎登录')
            break
        else:
            print('用户名/密码错误,还有%d次机会!' % (count,))
        count -= 1
    

    问题十:猜年龄游戏

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

    答:

    #!/user/bin/env python
    # -*- coding:utf-8 -*-
    count = 1
    while count <= 3:
        age = input('请输入猜测的年龄:')
        age = int(age)
        if age == 66:
            print('恭喜您,猜对了!')
            break
        else:
            print('猜错了!')
        count += 1
    

    问题十一:猜年龄游戏升级版

    要求:允许用户最多尝试3次,每尝试3次后,如果还没猜对,就问用户是否还想继续玩,如果回答Y,就继续让其猜3次,以此往复,如果回答N,就退出程序,如何猜对了,就直接退出。

    答:

    #!/user/bin/env python
    # -*- coding:utf-8 -*-
    count = 1
    while count <= 3:
        age = input('请输入猜测的年龄:')
        age = int(age)
        if age == 66:
            print('恭喜您,猜对了!')
            break
        else:
            print('猜错了!')
        if count == 3:
            choice = input('是否还想继续玩(Y/N):')
            if choice == 'Y':
                count = 1
                continue
            else:
                break
        count += 1
    

    问题十二:判断下列逻辑语句的True,False

    1 > 1 or 3 < 4 or 4 > 5 and 2 > 1 and 9 > 8 or 7 < 6
    not 2 > 1 and 3 < 4 or 4 > 5 and 2 > 1 and 9 > 8 or 7 < 6

    答:

    1 > 1 or 3 < 4 or 4 > 5 and 2 > 1 and 9 > 8 or 7 < 6 结果:True
    not 2 > 1 and 3 < 4 or 4 > 5 and 2 > 1 and 9 > 8 or 7 < 6 结果:False

    问题十三:求出下列逻辑语句的值。

    8 or 3 and 4 or 2 and 0 or 9 and 7
    0 or 2 and 3 and 4 or 6 and 0 or 3

    答:

    8 or 3 and 4 or 2 and 0 or 9 and 7 结果:8
    0 or 2 and 3 and 4 or 6 and 0 or 3 结果:4

    问题十四:下列结果是什么?

    6 or 2 > 1
    3 or 2 > 1
    0 or 5 < 4
    5 < 4 or 3
    2 > 1 or 6
    3 and 2 > 1
    0 and 3 > 1
    2 > 1 and 3
    3 > 1 and 0
    3 > 1 and 2 or 2 < 3 and 3 and 4 or 3 > 2

    答:

    6 or 2 > 1 结果:6
    3 or 2 > 1 结果:3
    0 or 5 < 4 结果:False
    5 < 4 or 3 结果:3
    2 > 1 or 6 结果:True
    3 and 2 > 1 结果:True
    0 and 3 > 1 结果:0
    2 > 1 and 3 结果:3
    3 > 1 and 0 结果:0
    3 > 1 and 2 or 2 < 3 and 3 and 4 or 3 > 2 结果:2

    问题十五:图形打印

    要求:使用while循环,完成以下图形的打印

    答:

    *
    * *
    * * *
    * * * *
    * * * * *
    * * * *
    * * *
    * *
    *
    
    #!/user/bin/env python
    # -*- coding:utf-8 -*-
    count_1 = 1
    count_2 = 4
    while True:
        if count_1 <= 5:
            print('* ' * count_1)
            count_1 += 1
        else:
            if count_2 >= 1:
                print('* ' * count_2)
                count_2 -= 1
            else:
                break
    

    问题十六:图形打印升级版

    要求:使用while循环,按照用户输入的最长行数,进行图形的打印,并可以多次玩。

    答:

    #!/user/bin/env python
    # -*- coding:utf-8 -*-
    while True:
        row = input('请输入最长行数:')
        if row.isdigit():
            row = int(row)
            count_1 = 1
            count_2 = row - 1
            while True:
                if count_1 <= row:
                    print('* ' * count_1)
                    count_1 += 1
                else:
                    if count_2 >= 1:
                        print('* ' * count_2)
                        count_2 -= 1
                    else:
                        break
            choice = input('是否继续玩(Y/N):')
            if choice.upper() == 'Y':
                continue
            else:
                break
        else:
            print('请输入数字')
    

    问题十七:假设一年期定期利率为3.25%,计算一下需要过多少年,一万元的一年定期存款连本带息能达到预期目标?

    #!/user/bin/env python
    # -*- coding:utf-8 -*-
    expect_amount = input('请输入期望金额:')
    expect_amount = int(expect_amount)
    count = 0
    principal= 10000
    while True:
        if principal <= expect_amount:
            principal *= 1.0325
            count += 1
        else:
            break
    print('达到期望金额:%d元,所需年限为%d年,最终金额为%d'%(expect_amount,count,principal,))
    
  • 相关阅读:
    AFO以后的机房游记
    THUSC2019 退役记
    最小树形图模板
    Codeforces Round #618 (Div. 2)
    「CF662C」 Binary Table
    「SCOI2012」喵星球上的点名
    P4480 「BJWC2018」「网络流与线性规划24题」餐巾计划问题
    CSP-S 2019 游记
    「BZOJ2839」集合计数
    「SPOJ 3105」Power Modulo Inverted
  • 原文地址:https://www.cnblogs.com/zhanglongfei/p/11575126.html
Copyright © 2020-2023  润新知