Python核心编程第二章课后习题

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






























评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值