您的位置:首页 > 编程语言

Pytho 4000 n核心编程第二章课后习题

2013-08-26 17:21 465 查看
2–5. 循环和数字

分别使用while 和for 创建一个循环:
(a) 写一个while 循环,输出整数从0 到10。(要确保是从0 到10, 而不是从0 到9 或从1 到10)

>>> i=0

>>> while i<11:

...     print i

...     i=i+1

...

(b) 做同 (a) 一样的事, 不过这次使用 range() 内建函数。

for i in range(0,11):

print i

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

def panduan()

x=raw_input("pleaase input one number:  ")

if int(x)<0:

print "this is -"

elif int(x)>0:

print "this is +"

else:

print "it is 0"

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

x=raw_input("please input something.....")

i=0

while i<len(x):

print x[i]

i=i+1

x=raw_input("please input something.....")

i=0

for i in x:
print i

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

# Using while loop

i = 0

total = 0

a = [1, 2, 3, 4, 5]

while (i < 5):

    print 'Please input number', i+1

    a[i] = float(raw_input())

    total = total + a[i]

    i = i + 1

print 'The total is', total

# Using for loop

total = 0

a = [1, 2, 3, 4, 5]

for i in range(0, 5):

    print 'Please input number', i+1

    a[i] = float(raw_input())

    total = total + a[i]

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

i = 0

total = 0

a = [1, 2, 3, 4, 5]

while (i < 5):

    print 'Please input number', i+1

    a[i] = float(raw_input())

    total = total + a[i]

    i = i + 1

print 'The average is', total / 5.

# Using for loop

total = 0

a = [1, 2, 3, 4, 5]

for i in range(0, 5):

    print 'Please input number', i+1

    a[i] = float(raw_input())

    total = total + a[i]

print 'The average is', total / 5.
2-10.

带循环和条件判断的用户输入。使用raw_input()函数来提示用户输入一个1和100之间的数,如果用户输入的数值满足这个条件,显示成功并退出。否则显示一个错误信息然后再次提示用户输入数值,直到满足条件为止。
>>> def one2hun():

...     t=1

...     while (t):

...             x=raw_input("please input one between 1-100: ")

...             if int(x)<1:

...                     print "x should >1"

...             elif int(x)>100:

...                     print "x should <100"

...             else:

...                     print "it is ok"

...                     t=0

...

【未完】这里并没有检查输入不是数字的情况。
2–11.带文本菜单的程序 写一个带文本菜单的程序,菜单项如下(1)取五个数的和 (2) 取五个数的平均值....(X)退出。由用户做一个选择,然后执行相应的功能。当用户选择退出时程序结束。这个程序的有用之处在于用户在功能之间切换不需要一遍一遍的重新启动你的脚本。(这对开发人员测试自己的程序也会大有用处)

 #!/usr/bin/python2

  2

  3 def to_total():

  4     i=0

  5     total=0

  6     a=[1,2,3,4,5]

  7     while i<5:

  8         print "please input the number ", i+1

  9         a[i]=float(raw_input())

 10         total=total+a[i]

 11         i=i+1

 12     print "the total is ",total

 13

 14 def to_avg():

 15     i=0

 16     total=0

 17     a=[1,2,3,4,5]

 18     while i<5:

 19         print "please input the number ", i+1

 20         a[i]=float(raw_input())

 21         total=total+a[i]

 22         i=i+1

 23     print "the average is ",total/5

 24

 25 support_command=('1','2','x')

 26

 27 def get_command():

 28     command=raw_input('''''

 29     ------------------------

 30     #please input command:#

 31     #1:               sum:#

 32     #2:               avg:#

 33     #x:              quit:#

 34     ------------------------

 35     ''')

 36     if command in support_command:

 37         return command  

 38     else:

 39         print "wrong command"

 40         get_command()

 41

 42 icommand=get_command()

 43 if icommand=='1':

 44     to_total()

 45 elif icommand=='2':

 46     to_avg()

 47 elif icommand=='x':

 48     pass
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: