Python程序设计(第三版)课后题答案 第六章
编程练习题
- 1.编写一个程序来打印歌曲“Old MacDonald”的歌词。你的程序应该打印五种不同动物的歌词,类似于下下面的例子。
Old MacDonald had a farm,Ee-igh,Ee-igh,Oh!
And on that farm he had a cow, Ee-igh,Ee-igh,Oh!
With a moo,moo here and a moo,moo there.
Here a moo, there a moo, everywhere a moo,moo.
Old MacDonald had a farm,Ee-igh,Ee-igh,Oh!
答案:
def lyrics(animal, cry):
for i in range(len(animal)):
print('Old MacDonald had a farm,Ee-igh,Ee-igh,Oh!')
print('And on that farm he had a {0}, Ee-igh,Ee-igh,Oh!'.format(animal[i]))
print('With a {0},{0} here and a {0},{0} there.'.format(cry[i]))
print('Here a {0}, there a {0}, everywhere a {0},{0}.'.format(cry[i]))
print('Old MacDonald had a farm,Ee-igh,Ee-igh,Oh!\n')
def print_lyrics(): # 打印歌词Old MacDonald
animal = ['cow', 'dog', 'cat', 'pig', 'sheep']
sound = ['moo', 'wang', 'miao', 'heng', 'mie']
lyrics(animal, sound)
print_lyrics()
输出结果:
Old MacDonald had a farm,Ee-igh,Ee-igh,Oh!
And on that farm he had a cow, Ee-igh,Ee-igh,Oh!
With a moo,moo here and a moo,moo there.
Here a moo, there a moo, everywhere a moo,moo.
Old MacDonald had a farm,Ee-igh,Ee-igh,Oh!
Old MacDonald had a farm,Ee-igh,Ee-igh,Oh!
And on that farm he had a dog, Ee-igh,Ee-igh,Oh!
With a wang,wang here and a wang,wang there.
Here a wang, there a wang, everywhere a wang,wang.
Old MacDonald had a farm,Ee-igh,Ee-igh,Oh!
Old MacDonald had a farm,Ee-igh,Ee-igh,Oh!
And on that farm he had a cat, Ee-igh,Ee-igh,Oh!
With a miao,miao here and a miao,miao there.
Here a miao, there a miao, everywhere a miao,miao.
Old MacDonald had a farm,Ee-igh,Ee-igh,Oh!
Old MacDonald had a farm,Ee-igh,Ee-igh,Oh!
And on that farm he had a pig, Ee-igh,Ee-igh,Oh!
With a heng,heng here and a heng,heng there.
Here a heng, there a heng, everywhere a heng,heng.
Old MacDonald had a farm,Ee-igh,Ee-igh,Oh!
Old MacDonald had a farm,Ee-igh,Ee-igh,Oh!
And on that farm he had a sheep, Ee-igh,Ee-igh,Oh!
With a mie,mie here and a mie,mie there.
Here a mie, there a mie, everywhere a mie,mie.
Old MacDonald had a farm,Ee-igh,Ee-igh,Oh!
进程已结束,退出代码0
- 2.写一个程序来打印“The Ants Go Marching.”十段的歌词。下面给出几个例句。你可以为每一节中的“little one”选择你自己的活动,但一定要选择一些押韵的内容。
The ants go marching one by one,hurrah! hurrah!
The ants go marching one by one,hurrah! hurrah!
The ants go marching one by one,
The little one stops to suck his thumb,
And they all go marching down…
In the ground…
To get out …
Of the rain.
Boom! Boom! Boom!
The ants go marching two by two,hurrah! hurrah!
The ants go marching two by two,hurrah! hurrah!
The ants go marching two by two,
The little one stops to suck his shoes,
And they all go marching down…
In the ground…
To get out …
Of the rain.
Boom! Boom! Boom!
答案:
def lyrics2(count, limbs):
for i in range(len(count)):
print('The ants go marching {0} by {0},hurrah! hurrah!\nThe ants go marching {0} by {0},hurrah! hurrah!\nThe '
'ants go marching {0} by {0},\nThe little one stops to suck his {1},\nAnd they all go marching '
'down...\nIn the ground...\nTo get out ...\nOf the rain.\nBoom! Boom! Boom! \n'.format(count[i], limbs[i]))
def print_lyrics(): # 打印歌词The ants go marching
number = ['one', 'two', 'three']
body = ['thumb', 'shoe', 'arm']
lyrics2(number, body)
print_lyrics()
输出结果:
The ants go marching one by one,hurrah! hurrah!
The ants go marching one by one,hurrah! hurrah!
The ants go marching one by one,
The little one stops to suck his thumb,
And they all go marching down...
In the ground...
To get out ...
Of the rain.
Boom! Boom! Boom!
The ants go marching two by two,hurrah! hurrah!
The ants go marching two by two,hurrah! hurrah!
The ants go marching two by two,
The little one stops to suck his shoe,
And they all go marching down...
In the ground...
To get out ...
Of the rain.
Boom! Boom! Boom!
The ants go marching three by three,hurrah! hurrah!
The ants go marching three by three,hurrah! hurrah!
The ants go marching three by three,
The little one stops to suck his arm,
And they all go marching down...
In the ground...
To get out ...
Of the rain.
Boom! Boom! Boom!
进程已结束,退出代码0
- 3.写出这些函数的定义:
sphereArea(radius)返回具有给定半径的球体的表面积。
sphereVolume(radius)返回具有给定半径的球体的体积。
使用你的函数来解决第三章中的编程练习1.
答案:
def sphereArea(radius):
return 4 * pi * radius ** 2
def sphereVolume(radius):
return 4 / 3 * pi * radius ** 3
def calculate():
r = eval(input("请输入球体的半径:"))
a = sphereArea(r)
v = sphereVolume(r)
print