博客园  :: 首页  :: 新随笔  :: 联系 :: 订阅 订阅  :: 管理

第九节 Python基础之练习(案例来自菜鸟教程)

Posted on 2017-02-17 10:22  Jasonhy  阅读(163)  评论(0)    收藏  举报
# 需求:有四个数字:1、2、3、4,能组成多少个互不相同且无重复数字的三位数?各是多少?
def make_three_num():
    '''
    思路:先拿出每一个数位的数字,然后再判断每个数位是否相同,不相同的话,通过字符串拼接成三位数,放在一个数组里边
    :return:
    '''
    arr = []
    for i in range(1,5):
        for j in range(1,5):
            for k in range(1,5):
                if (i != j) and (i != k) and (j != k):
                    s_str = "%s%s%s"%(i,j,k)                # 我感觉这种效率要高一点
                    # s_str = "{}{}{}".format(i,j,k)
                    arr.append(s_str)
    return arr
res = make_three_num()
print("个数%d "%len(res),"组成的三位数有%s"%res)

 

# 需求:企业发放的奖金根据利润提成。利润(I)低于或等于10万元时,奖金可提10%;利润高于10万元,
# 低于20万元时,低于10万元的部分按10%提成,高于10万元的部分,可提成7.5%;20万到40万之间时,
# 高于20万元的部分,可提成5%;高于40万元时,超过40万元的部分按1%提成,从键盘输入当月利润I,求应发放奖金总数?
def get_bonus():
    while True:
        profit = input("请输入本年利润: ")
        if profit == "exit":
            break
        elif not str.isnumeric(profit):
            print("请输入数字!!!")
            continue
        else:
            profit = int(profit)
            if profit <= 10:
                print("本年的奖金为%s:" % (profit * 0.1))
            elif (profit > 10) and (profit < 20):
                print("本年的奖金为%s: " % ((10 * 0.1) + (profit - 10) * 0.075))
            elif (profit > 20) and (profit < 40):
                print("本年的奖金为%s: " % ((10 * 0.1) + (profit - 10) * 0.075 + (profit - 20) * 0.05))
            else:
                print("本年的奖金为%s: " % ((10 * 0.1) + (profit - 10) * 0.075 + (profit - 20) * 0.05 + (profit-40) * 0.01))
get_bonus()

 

# 需求:输入某年某月某日,判断这一天是这一年的第几天?
def get_data():
    # 定义一个元组存放天数
    months = (0, 31, 59, 90, 120, 151, 181, 212, 243, 273, 304, 334)
    year = int(input("请输入年份: \n"))
    month = int(input("请输入月份: \n"))
    day = int(input("请输入日: \n"))
    d_sum = 0
    if 0 < month <= 12:
        d_sum = months[month - 1]
    else:
        print("月份有误")
    d_sum += day
    # 用来判断是闰年还是平年 是否需要加1
    leap = 0
    # 如果是闰年 赋1
    if (year % 400 == 0) or ((year % 4 == 0) and (year % 100 != 0)):
        leap = 1
    # 如果是闰年 判断月是否大于2
    if (leap == 1) and (month > 2):
        d_sum += 1

    return d_sum
print(get_data())
# 需求:获取第8个斐波那契数列的值 指的是这样的数列:1、1、2、3、5、8、13、21、34...
def get_series():
    result = 0
    num = 0
    for i in range(10):
        if i == 0:
            temp = 1
        else:
            temp = num
        num = result
        result = num + temp
    return result

print(get_series())