python核心编程第二版第五章习题解析

5.1
表达上:长整型+L
内容上:长整型表示的数字更大
5.2
(a)

def product(a,b):
    print("the consequence:",a*b)

(b)
5.3

c = int(input("please input a number:"))
if c>=90 and c<=100:
    print("A")
elif c>=80 and c<=89:
    print("B")
elif c>=70 and c<=79:
    print("C")
elif c>=60 and c<=69:
    print("D")
else:
    print("F")

5.4

c = int(input("please input the year:"))
if c%4==0:
    print("this year is leap year ")
elif c%4==0 and c%100==0:
    print("this year is the leap year")
else:
    print("this year is not a leap year")

5.5美元换算成美分

#美元换算成美分
c = int(input("please input the number:"))
A = divmod(c,20)
if A[1]>1:
    B = divmod(A[1],10)
    if B[1]>1:
        C = divmod(B[1],5)
        print("this can be converted %s 20cents,%s 10cents %s 5cents %s 1cents" %(A[0],B[0],C[0],C[1]))
    else:
        print("this can be converted %s 20cents,%s 10cents %s 1cents" % (A[0], B[0], B[1]))
else:
    print("this can be converted %s 20cents, %s 1cents" % (A[0], A[1]))

(蹩脚英语表达见谅 ?)
5.6

def test(a,s,b):
    if s == "+":
        print(a+b)
    elif s == "-":
        print(a-b)
    elif s == "*":
        print(a*b)
    elif s == "%":
        print(a%b)
    elif s == "/":
        print(a/b)
    elif s == "//":
        print(a//b)
    elif s == "**":
        print(a**b)
    else:
        print("invalid expression! ")
test(1,"=",4)

5.7
应该是用switch吧
5.8
计算面积和体积

import math
print("1正方形\n2立方体\n3圆\n4球")
choice=input("please input your choice:")
length =int(input("please input the length:"))
if choice == "1":
    # length = input("please input the length:")
    print("正方形的面积:", length*length)
if choice == "2":
    # length = input("please input the length:")
    print("立方体的面积:", 6*length*length)
    print("立方体的体积:", length**3)
if choice == "3":
    # length = input("please input the length:")
    print("圆的面积:", length*length*math.pi)
if choice == "4":
    print("球的体积:", 4/3*length**3*math.pi)

5.17

import random
N = random.randint(2,100)
print("N:",N)
list = []
for i in range(N):
    n = random.randint(0,2**31-1)
    list.append(n)
    i=i+1
list.sort()
print(list)
  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值