python核心编程课后题第二版36页第二章

2-5. 循环和数字。
分别使用while和for创建一个循环。
(a)写一个while循环,输出整型为0~10(要确保是0~10,而不是0~9或1~10)。
(b)做同(a)一样的事,不过这次使用range()内建函数。

(a)

#coding = utf-8
n = 0
while n < 11:
    print n
    n += 1

 

如果想让输出在同一行,则需要在print n之后加上逗号,为以下代码:

#coding = utf-8
n = 0
while n < 11:
    print n,
    n += 1

 

(b)

#coding = utf-8
#while
n = 0
while n in range(0, 11):
    print n,
    n += 1

print '\n'
#for
for b in range(0, 11):
    print b,

 

感觉这种循环for更方便一点。

2-6 条件判断。判断一个数是正数,还是负数,或者是0。开始先用固定的数值,然后修改你的代码支持用户输入数值再进行判断。

 

#coding=utf-8
n = 123
if n < 0:
    print 'fushu'
elif n > 0:
    print 'zhengshu'
else:
    print 'zero'


num = raw_input('Please enter a number>>')
try:
    num = int(num)
    if num < 0:
        print 'fushu'
    elif num > 0:
        print 'zhengshu'
    else:
        print 'zero'
except:
    print 'Please enter the number, not string or others.'

2-7 循环和字串。从用户那里接受一个字符串输入,然后逐字符显示该字符串。先用while循环实现,然后再用for循环实现。

#coding=utf-8
string = raw_input('Please enter a string>>')
n = 0
while n < len(string):
    print string[n],
    n += 1

print '\n'
for letter in string:
    print letter,

2-8 循环和操作符。创建一个包含五个固定数值的列表或元组,输出他们的和。然后修改你的代码为接受用户输入数值。分别使用while和for循环实现。

#coding=utf-8
li = [1, 2, 5, 7, 12]
#while
total = 0
n = 0
while n < len(li):
    total += li[n]
    n += 1
print total

#for
total = 0
for num in li:
    total += num;
print total

下面是接受用户输入数值的模式:

#coding=utf-8
li = [1, 2, 3, 4, 5]
#while
total = 0
n = 0
while n < len(li):
    li[n] = raw_input('Enter No.%d>>' %n)
    total += int(li[n])
    n += 1
print total

#for
total = 0
for num in li:
    num = raw_input('Enter>>')
    total += int(num)
print total

加入了对输入字符的判断:

#coding=utf-8
li = [1, 2, 3, 4, 5]
#while
total = 0
n = 0
while n < len(li):
    while 1:
        li[n] = raw_input('enter no.%d>>' %n)
        try:
            li[n] = int(li[n])
            break
        except:
          print 'Please enter a number.'
    total += int(li[n])
    n += 1
print total

#for
total = 0
for num in li:
    while 1:
        num = raw_input('Enter>>')
        try:
            num = int(num)
            break
        except:
            print 'Please enter a number.'

    total += int(num)
print total

PS,老婆催着回去,逻辑还没搞得彻底清楚。唉~

2012年12月3日补充:

2-9 循环和操作符。创建一个包含五个固定数值的列表或元组,输出他们的平均值。本练习的难点之一使通过除法得到平均值。你会发现整型除会截去小数,因此你必须使用浮点除以得到更精确的结果。float()内建函数可以帮助你实现这一功能。

#coding=utf-8
a = (2, 4, 6, 9, 11)
total = 0
for n in a:
    total += n

total = float(total)
print total / 5

这里有个问题,如果不对total进行浮点型转化,只在最后输出时转化是没有效果的,如下代码:

1 #coding=utf-8
2 a = (2, 4, 6, 9, 11)
3 total = 0
4 for n in a:
5     total += n
6 
7 print float(total / 5)

最后会输出6.0,仍不精确。

当然,如果换成while循环也可以实现,有兴趣的可以试试。

2-10 带文本菜单的程序写一个带文本菜单的程序,菜单项如下:(1)取五个数的和;(2)取五个数的平均值...(X)退出。由用户做一个选择,然后执行相应的功能。当用户选择退出时程序结束。这个程序的有用之处在于用户在功能之间切换不需要一遍一遍地重新启动你的脚本(这对开发人员测试自己的程序程序也大有好处)。

 

#coding=utf-8
def sum_num(li):
    total = 0
    for n in li:
        total += n
    return total

def ave_num(li):
    total = 0
    for n in li:
        total += n
    total = float(total)
    return total / 5

a = [3, 9, 11, 5, 14]

print 'Hi, that are three choice here: <1> show the sum of 5 numbers. <2> show the average of 5 numbers. <3> exit.'
while 1:
    choice = raw_input('Please enter the number of choice>>')
    if choice == '1':
        print sum_num(a)
    elif choice == '2':
        print ave_num(a)
    elif choice == '3':
        break
    else:
       print 'Enter errors.'

PS,请忽略我的巩义本土英语。

2-11 ~ 2-14题目没写,可以看这个文章:http://www.cnblogs.com/balian/archive/2011/01/12/1934016.html

2-15 元素排序。
让用户输入3个数值并分别将他们保存到3个不同的变量中。不使用列表或排序算法,自己写代码来对3个数由小到大排序。(b)修改(a)的解决方案,使之从大到小排列。

 

#coding=utf-8
a = raw_input('Enter the valure of "a">>')
b = raw_input('Enter the valure of "b">>')
c = raw_input('Enter the valure of "c">>')

a = int(a)
b = int(b)
c = int(c)

if a > b:
    n = a
    a = b
    b = n
if a > c:
    n = a
    a = c
    c = n
if b > c:
    n = b
    b = c
    c = n
print a, b, c

PS,只写了从小到大的部分,从大到小的部分原理一样。

补充:

刚看了下一章3.2.4中多元赋值的方法,python真是太简洁了,b, a = a, b就能表示a和b替换过了,下面是代码:

#coding=utf-8
a = raw_input('Enter the valure of "a">>')
b = raw_input('Enter the valure of "b">>')
c = raw_input('Enter the valure of "c">>')

a = int(a)
b = int(b)
c = int(c)

if a > b:
    a, b = b, a
if a > c:
    a, c = c, a
if b > c:
    b, c = c, b
print a, b, c

posted on 2012-12-02 18:21  alexkh  阅读(239)  评论(0编辑  收藏  举报