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

2-9

__author__ = 'Yuriy'

li = [12,43,12,6,280]
i = 0
values = 0
for i in range(len(li)):
   values += li[i]
print values / float(len(li))

Attention : 除法中被除数为浮点数时,结果也为浮点数

2-10

__author__ = 'Yuriy'
while True:
   ch = raw_input(">>>")
   if not ch.isdigit():
      print 'Please input a number!'
   elif 1 < int(ch) < 100:
      print 'Success .You have input the number :%d' % int(ch)
   else:
      print 'the number must between 1~100'

>>>type
Please input a number!
>>>-20
Please input a number!
>>>689
the number must between 1~100
>>>39
Success .You have input the number :39

2-11

# coding = utf-8
__author__ = 'Yuriy'
def Add5():
   li = []
   while True:
      ch = raw_input(">>>")
      if ch == 'end':
         break
      elif ch == 'help':
         print 'Enter \'end\' to finish the input' 
     elif ch.isdigit():
         li.append(ch)
      else:
         print 'Please input numbers!'
   print li
   i = 0
   values = 0
   for i in range(len(li)):
      values += int(li[i])
      i += 1
   print values

def Average5():
   li = []
   while True:
      ch = raw_input(">>>")
      if ch == 'end':
         break
      elif ch == 'help':
         print 'Enter \'end\' to finish the input'
      elif ch.isdigit():
         li.append(ch)
      else:
         print 'Please input numbers!'
   print li
   i = 0
   values = 0
   for i in range(len(li)):
      values += int(li[i])
      i += 1
   print values / float(len(li))

while True:
   print '''
   (1)Function Add
   (2)Function Div
   (X)Exit   '''
   ch = raw_input("please select the function:")
   if ch == 'X':
      break
   elif int(ch) == 1:
      Add5()
   elif int(ch) == 2:
      Average5()
   else:
      print 'please input correct option !'


    (1)Function Add
    (2)Function Div
    (X)Exit
    
please select the function:1
>>>20
>>>43
>>>276
>>>2
>>>7
>>>end
['20', '43', '276', '2', '7']
348

    (1)Function Add
    (2)Function Div
    (X)Exit
    
please select the function:2
>>>29
>>>54
>>>234
>>>2
>>>76
>>>end
['29', '54', '234', '2', '76']
79.0

    (1)Function Add
    (2)Function Div
    (X)Exit
    
please select the function:X

Process finished with exit code 0

文中有中文字符时,提示出错,
暂时先用英文替代

2-12

  • (a) 输出['builtins', 'doc', 'name', 'package']
  • (b) 显示对象的属性,如果没有参数,则显示全局变量的名字
    不加括号,返回,表示dir是内建的函数
  • (c) 返回,表示dir是一个内部已经建立的功能或方法
  • (d) 文档字符串的提取。

2-13

  • (a) 导入 sys 后,可以看到dir() 显示的全局变量多了一个sys
    dir(sys) 查看sys的所有方法及变量
  • (b) sys.platform -->win32
    sys.version -->'2.7.10 (default, May 23 2015, 09:40:32) [MSC v.1500 32 bit (Intel)]'
  • (c) sys.exit() 类似win 终端中的exit

2-14

我就没找到2.4小节哪个print语句是没有正常工作的!!!

2-15

难点:不使用列表怎么生成多个变量名称?(未完成)

__author__ = 'Yuriy'
i = 0
li = []
print 'Please input three numbers'
while i < 3:
   ch = raw_input(">>>")
   try:
      li.append(int(ch))
      i += 1
   except ValueError, e:
      print 'Please input number!'
if li[0] < li[1]:
   if li[1] < li[2]:
      print "%d < %d < %d" % (li[0], li[1], li[2])
   elif li[0] < li[2]:
      print "%d < %d < %d" % (li[0], li[2], li[1])
   elif li[0] > li[2]:
      print "%d < %d < %d" % (li[2], li[0], li[1])
   else:
      print "%d = %d < %d" % (li[0], li[2], li[1])
elif li[0] > li[1]:
   if li[0] < li[2]:
      print "%d < %d < %d" % (li[1], li[0], li[2])
   elif li[1] < li[2]:
      print "%d < %d < %d" % (li[1], li[2], li[0])
   elif li[1] > li[2]:
      print "%d < %d < %d" % (li[2], li[1], li[0])
   else:
      print "%d = %d < %d" % (li[2], li[1], li[0])
else:   if li[0] < li[2]:
      print "%d = %d < %d" % (li[1], li[0], li[2])
   elif li[0] > li[2]:
      print "%d < %d = %d" % (li[2], li[1], li[0])
   else:
      print "%d = %d = %d" % (li[2], li[1], li[0])
Please input three numbers
>>>5
>>>8
>>>23465
5 < 8 < 23465

好笨的写法、后面看怎么样改进

2-16

# coding = UTF-8
__author__ = 'Yuriy'
filename = raw_input("Enter file name:\n")
fobj = open(filename ,'r')
for eachLine in fobj:
   print eachLine,fobj.close()
Enter file name:
D:/abc.txt
学号:A01214001    姓名:戴涤川
学号:A01214002    姓名:马兵燕
学号:A01214003    姓名:孙振鹏
学号:A01214004    姓名:马玺渊
学号:A01214005    姓名:贲顺琦
学号:A01214006    姓名:郑岳

注意:输入win目录的时候 反斜杠\ 应为 正斜杠/

你可能感兴趣的:(Python核心编程课后习题-第二章2)