python核心编程2参考答案(第五章)

1、Name the differences between Python’s regular and long integers

普通整型和长整型主要是可以表示的数据位数不同

2、Operators. 
(a) Create a function to calculate and return the product of 
two numbers. 
(b) The code which calls this function should display the 
result.

# _*_ coding= utf-8 _*_
def PlusTwo(a,b):
	return a * b
print PlusTwo(2,3)
print PlusTwo(3,'a')

结果是:

6
aaa

3、Standard Type Operators. Take test score input from the user 
and output letter grades according to the following grade 
scale/curve:
A: 90–100
B: 80–89
C: 70–79
D: 60–69
F: <60

函数体在此:

# _*_ coding= utf-8 _*_
def scoreOut(x):
	scoreDic = {9:"A",8:"B",7:"C",6:"D"}
	score  = x / 10
	for item in sorted(scoreDic.keys(),reverse = True):
	    if score >= item:
	        out = scoreDic[item]
	        break
	    else:
	        out = "F"
	return out


if __name__ == "__main__":
	while True:
    		score = input("Enter your number,Please!")
    		print "Your level is: %s" % scoreOut(score)
    		Choose = raw_input('你还想继续查吗?Y/N')
    		if Choose == 'Y':
    			continue
    		else:
    			break

这里多了while的判断,这样就可以多次输入。

执行结果如下:

Enter your number,Please!99
Your level is: A
你还想继续查吗?Y/NY
Enter your number,Please!21
Your level is: F
你还想继续查吗?Y/N


4、Modulus. Determine whether a given year is a leap year, 
using the following formula: a leap year is one that is divisible 
by four, but not by one hundred, unless it is also divisible by 
four hundred. For example, 1992, 1996, and 2000 are leap 
years, but 1967 and 1900 are not. The next leap year falling 
on a century is 2400.

# _*_ coding= utf-8 _*_
def leastCoin(coinNum):
        #coin 25,10,5,1
        #Get the least coin number
        coin = [25,10,5,1]
        coinN = []
        for item in coin:
                coinN.append(coinNum / item)
                coinNum = coinNum % item
        return coinN

if __name__ == "__main__":
        coin = input("Enter your coin num,Please!")
        coinN = leastCoin(coin)
        print "The least coin for %d is 25:%d,10:%d,5:%d,1:%d" % (coin,coinN[0],coinN[1],coinN[2],coinN[3])

5、Modulus. Calculate the number of basic American coins 
given a value less than 1 dollar. A penny is worth 1 cent, a 
nickel is worth 5 cents, a dime is worth 10 cents, and a 
quarter is worth 25 cents. It takes 100 cents to make 1 dollar. 
So given an amount less than 1 dollar (if using floats, con-
vert to integers for this exercise), calculate the number of 
each type of coin necessary to achieve the amount, maxi-
mizing the number of larger denomination coins. For 
example, given $0.76, or 76 cents, the correct output would 
be “3 quarters and 1 penny.” Output such as “76 pennies” 
and “2 quarters, 2 dimes, 1 nickel, and 1 penny” are not 
acceptable

# _*_ coding= utf-8 _*_
def leastCoin(coinNum):
        #coin 25,10,5,1
        #Get the least coin number
        coin = [25,10,5,1]
        coinN = []
        for item in coin:
                coinN.append(coinNum / item)
                coinNum = coinNum % item
        return coinN


if __name__ == "__main__":
        coin = input("Enter your coin num,Please!")
        coinN = leastCoin(coin)
        print "The least coin for %d is 25:%d,10:%d,5:%d,1:%d" % (coin,coinN[0],coinN[1],coinN[2],coinN[3])

9、 Style. Answer the following numeric format questions:
(a) Why does 17 + 32 give you 49, but 017 + 32 give you 47 
and 017 + 032 give you 41, as indicated in the examples 
below?
>>> 17 + 32
49
>>> 017+ 32
47
>>> 017 + 032
41
(b) Why do we get 134L and not 1342 in the example below?
 >>> 56l + 78l
134L

12、写一段脚本确认一下你的 Python 所能处理的整数,长整数,浮点数和复

数的范围。

import sys
dir(sys)
sys.maxint
sys.float_info
#在交互解释器运行这些语句,会看到想要的信息

14、Bank Account Interest. Create a function to take an interest 
percentage rate for a bank account, say, a Certificate of 
Deposit (CD). Calculate and return the Annual Percentage 
Yield (APY) if the account balance was compounded daily.

# _*_ coding= utf-8 _*_
def Onerus(i):
	F = (1 + float(i)/365) ** 365
	print F
Onerus(3)

17、Random Numbers. Read up on the random module and do 
the following problem: Generate a list of a random number 
(1 < N <= 100) of random numbers (0 <= n <= 231 -1). 
Then randomly select a set of these numbers (1 <= N <= 100), 
sort them, and display this subset.

# _*_ coding= utf-8 _*_
import random
#random -- the module to generate random number

num = random.randint(1,100)
lst = []
for item in range(num):
        tmp = random.randint(0,(pow(2,31) - 1))
        lst.append(tmp)
lst.sort()
print lst


评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值