该练习属个人愚见,若在之后的学习中发现有更好的写法,会随时更新。
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))