Python核心编程第二版 -5.9练习

该练习属个人愚见,若在之后的学习中发现有更好的写法,会随时更新。

5.2:

# 5-2
def product(a,b):
    return a*b

print(product(1,2))

5.3:

#5-3
def test():
    score = int(input("Please input score:"))
    if score//10 == (9 or 10):
        return 'A'
    elif score // 10 == 8:
        return 'B'
    elif score // 10 == 7:
        return 'C'
    elif score // 10 == 6:
        return 'D'
    else:
        return 'F'

print(test())

5.4:

#5-4
def Year():
    year =  int(input("Please input year:"))
    if year % 4 != 0 :
        return "非闰年"
    elif (year % 4 == 0) & (year % 100 != 0):
        return "闰年"
    else:
        if year % 400 == 0:
            return "世纪闰年"
        else:
            return "非闰年"

print(Year())

5.5:

#5-5
import random

Total = random.randint(1,100)
print(Total)
List = [25,10,5,1]

i = 0
while Total % List[i] != 0:
    print(Total // List[i], '个', List[i], 'cent')
    Total = Total - (List[i] * (Total // List[i]))
    i += 1
print(Total // List[i],'个', List[i],'cent')
#5-5
import random

Total = random.randint(1,100)
print(Total)
List = [25,10,5,1]

for i in range(4):
    if Total % List[i] != 0:
        print(Total//List[i],'个',List[i],'cent')
        Total = Total - (List[i] * (Total//List[i]))
    if Total % List[i] == 0:
        print(Total // List[i],'个', List[i],'cent')
        break

5.11:

#5-11
a = [int(input("a =")),int(input("b ="))]
if (a[0] % a[1] == 0) or (a[1] % a[0] == 0):
    print(True)
else:
    print(False)

5-17:

#5-17
import random

N = random.randint(1,100)
random_list = [random.randint(0,(2**31-1)) for i in range(N)]  #随机序列生成
sample_list = random.sample(random_list,random.randint(1,N))   #对该随机序列随机抽取K个值
print(sorted(sample_list))

 

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值