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
<b>文中有中文字符時,提示出錯,
暫時先用英文替代</b>
2-12
- (a) 輸出['builtins', 'doc', 'name', 'package']
- (b) 顯示對象的屬性,如果沒有參數,則顯示全局變量的名字
不加括號,返回<built-in function dir>,表示dir是內建的函數 - (c) 返回<type 'builtin_function_or_method'>,表示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目錄的時候 反斜杠\ 應為 正斜杠/