2 – 2. P 程序输出, 阅读下面的 Python 脚本:
#!/usr/bin/env python
1 + 2 * 4
(a) 你认为这段脚本是用来做什么的?
(b) 你认为这段脚本会输出什么?
(c) 输入以上代码,并保存为脚本,然后运行它。它所做的与你的预期一样吗?为什么一
样/不一样?
(d) 这段代 独 码单 执行和在交互解释器中执行有何不同?试一下,然后写出结果
(e) 如何改 个 进这脚本, 以便它能和你想像的一样工作?
【答】
(a) 计算 1+2*4
(b) 在cmd中输出9
(c) 仅运行不输出。
(d) 交互式解释器中输出9
(e) 需要加print--> print 1+2*4
2–3. 数值和运算符
启动交互解释器,使用 Python 两个数值(任意类型)进行加、减、乘、除运算。然后使
用取余运算符来得到两个数相除的余数, 最后使用乘方运算符求 A数的 B 次方。
【答】
>>>def add(a,b):
... “cannot concatenate 'str' and 'int' objects”
... return a+b
>>>def sub(a,b):
... return a-b
>>>def multi(a,b):
... return a*b
>>>def div(a,b):
... if b != 0:
... return a*1.0/b
... else:
... return 0
>>>def mod(a,b):
... return a%b
>>>def pow(a,b):
... return a**b
2–4. 使用raw_input()函数得到用户输入
(a) 创建一段脚本使用 raw_input() 内建函数从用户输入得到一个字符串,然后显示个 这用户刚刚键入的字符串。
(b) 添加一段类似的代码,不过这次输入的是数值。将输入数据转换为一个数值对象,(使
用 int()或其它数值转换函数) 并将 个 这值显示给用户看。(注意,如果你用的是早于 1.5 的版本,你需要使用 string.ato*() 函数执行种 这转换)
【答】
(a)
>>>print(raw_input("Enter Your String : "))
Enter Your String : Hello World!
Hello World!
(b)
>>>import string
>>>print(int(string.atof(raw_input("Enter a number : "))))
Enter a number : 1321.123
1321
2–5. 循环和数字 分别使用 while 和 for创建一个循环:
(a) 写一个while 循环,输出整数从0 到 10。(要确保是从0 到 10, 而不是从0到9或
从1 到 10)
(b) 做同 (a) 一样的事, 不过这次使用 range() 内建函数。
【答】
(a)
>>>i = 0
>>>while i<=10:
... print i,
... i += 1
...
0 1 2 3 4 5 6 7 8 9 10
(b)
>>>i = 0
>>>for i in range(11):
... print i,
...
0 1 2 3 4 5 6 7 8 9 10
2–6. 条件判断判断一个数是正数,还是数 负, 或者等于 0. 开始先用固定的数值,然
后修改你的代码支持用户输入数值再进行判断。
【答】
>>>import string
>>>def isPositive():
... num = string.atof(raw_input(“Enter the number which you want to check :”))
... If num > 0:
... print ‘It is a positive!’
... elif num < 0:
... print ‘It is a negtive!’
... else:
... print ‘It is zero’
...
>>>isPositive()
Enter the number which you want to check: 4
It is a positive!
>>>isPositive()
Enter the number which you want to check: -4
It is a negtive!
>>>isPositive()
Enter the number which you want to check: 0
It is zero
2–7.循环和字串 从用户那里接受一个字符串输入,然后逐字符显示该字符串。先用 while 循
环实现,然后再用 for 循环实现。
【答】
>>>string = raw_input(“Enter your string: ”)
Enter your string: abcdefg
>>>i = 0
>>>while i < len(string):
... print i,
... i+=1
...
a b c d e f g
>>>for eachChar in string:
... print eachChar,
...
a b c d e f g
2–8. 循环和运算符 创建一个包含五个固定数值的列表或元组,输出他们的和。然后修
改你的代码为接受用户输入数值。 分别使用 while 和 for 循环实现。
【答】
>>>sum = 0
>>>i = 0
>>>numList = list(raw_input(“Enter the number list without any character: ”))
Enter the number list without any character: 12345
>>>while i < len(numList):
... sum += int(numList[i])
... i += 1
...
>>>sum
15
>>>sum = 0
>>>for i in numList:
... sum+=int(i)
...
>>>sum
15
2–9.循环和运算符 创建一个包含五个固定数值的列表或元组,输出他们的平均值。本练习的难
点之一是通过除法得到平均值。 你会发现整数除会截去小数,因此你必须使用浮点除以得到更精确的结果。 float()内建函数可以帮助你实现这一功能。
【答】
>>>numberList = [1,2,3,4,6]
>>>sum = 0
>>>for i in numberList:
... sum+=i
...
>>>ave = sum*1.0/len(numberList)
>>>ave
3.2
2–10. 带循环和条件判断的用户输入 使用 raw_input()函数来提示用户输入一个1 和 100 之间的数,如果用户输入的数满足个条 这 件,显示成功并退出。否则显示一个错误信息然后再次提示用户输入数值,直到满足条件为止。
【答】
>>>import string
>>>def fun():
... num = int(string.atof(raw_input(“Enter a number: ”)))
... if num <= 1 or num >= 100:
... print ‘please enter a right number which between 1 and 100: ’
... fun()
... else:
... print ‘Successful, exiting of the function...’
...
>>>fun()
Enter a number: -3
please enter a right number which between 1 and 100:
Enter a number: 111
please enter a right number which between 1 and 100:
Enter a number: 3
Successful, exiting of the function...
2–11. 带文本菜单的程序 写一个带文本菜单的程序,菜单项如下(1)取五个数的和 (2) 取五个数的平均值....(X)退出。由用户做一个选择,然后执行相应的功能。当用户选择退出时程序结束。个 这程序的有用之处在于用户在功能之间切换不需要一遍一遍的重新启你动的脚本。(这开 对发人员测试自己的程序也会大有用处)
【答】
>>>import string
>>>def fun(list):
...notice = ‘’’please enter a character to choose the function:
...‘1’ : get the sum of the list
...‘2’ : get the average of the list
...‘x’ : exit of the function
...: ’’’
... sum = 0
.... ave = 0
... i = 0
... choose = raw_input(notice)
... if choose == ‘1’:
... for i in list:
... sum += string.atof(i)
... print ‘the sum of the list is :’,
... print sum
... fun(list)#这里不用写sum = 0,因为sum定义在fun(list)里面
... elif choose == ‘2’:
... for i in list:
... sum += string.atof(i)
... ave = sum/len(list)
... elif choose == ’x’:
... print ‘exiting of the function’
... else:
... print 'pls enter a right character to choose the function: '
... fun(list)
...
>>> fun(numberList)
please enter a character to choose the function
'1' : get the sum of the list
'2' : get the average of the list
'x' : exit of the function
: 1
the sum of the list is : 16.0
please enter a character to choose the function
'1' : get the sum of the list
'2' : get the average of the list
'x' : exit of the function
: 2
the average of the list is : 3.2
please enter a character to choose the function
'1' : get the sum of the list
'2' : get the average of the list
'x' : exit of the function
: 3
pls enter a right character to choose the function:
please enter a character to choose the function
'1' : get the sum of the list
'2' : get the average of the list
'x' : exit of the function
: x
exiting of the function
2–12. dir()内建函数
(a) 启动Python 交互式解释器, 通过直接键入dir()回车以执行 dir()内建函数。你看到
什么? 显示你看到的每一个列表元素的值,记下实际值和你想像的值
(b) 你会问, dir()函数是干什么的?我们已经知道在 dir 后边加上一对括号可以执行dir()
内建函数, 如果不加括号会如何? 试一试。 解释器返回你 给什么信息? 你个认为这信息表示什么意思 ?
(c) type() 内建函数接收任意的 Python对象做 参数并 为 返回他们的类型。 在解释器中键入 type(dir), 看看你得到的是什么?
(d) 本练习的最后一部分, 我来瞧 们 一瞧Python的文档字符串。 通过 dir.__doc__ 可以访问dir()内建函数的文档字符串。print dir.__doc__可以显示个 这字符串的内容。 许多内建函数,方法,模块及模属 块性都有相应的文档字符串。我们希望你在你的代码中也要写 书文档字符串, 它会对使用这些代码的人提供及时方便的帮助。
2–13. 利用 dir() 找出 sys 模块中更多的东西。
(a) 启动Python 交互解释器, 执行dir()函数,然后键入 import sys 以导入 sys 模块。再次执行 dir()函数以确认sys 模块被正确的导入。 然后执行 dir(sys) , 你就可以看到 sys模块的所有属性了。
(b) 显示 sys 模块的版本号属性及平台变量。记住在属性名前一定要加 sys. ,这表示
个属 这 性是 sys 模块的。其中 version变量保存着你使用的 Python 解释器版本, platform 属性则包含你运行Python时使用的计算机平台信息。
(c) 最后, 调用 sys.exit() 函数。 这是一种热键之外的另一种退出 Python 解释器的方式 。
2–14. 重写2.4 小节中 print 语句里的算术表达式, 试着在个 这表达式中添加合适的
括号以便它能正常工作。
【答】2.12-2.14 略
2–15. 元素排序
(a)让用户输入三个数 并将 值 分将它 别们保存到3个不同的变量中。不使用列表或排序算法,自己写代来 码对这三个数由小到大排序。(b)修改(a)的解决方案,使之从大到小排序
【答】
>>>import string
>>>def bts():
... “(b)只要将每个排序倒着打即可,例如first>second>third改成third<second<first”
... first = string.atof(raw_input(“Enter the first number : ”))
... second = string.atof(raw_input(“Enter the second number : ”))
... third = string.atof(raw_input(“Enter the third number : ”))
... if first > second:
... if second > third:
... print ‘first > second > third’
... else:
... if first > third:
... print ‘first > third > second’
... else:
... print ‘third > first > second’
... else:
... if first < third:
... if second > third:
... print ‘second > third > first’
... else:
... print ‘third > second > first’
... else:
... print ‘second > first > third’
...
>>>bts()
Enter the first number : 4
Enter the second number : 3
Enter the third number : 6
third > first > second
>>>bts()
Enter the first number : 87
Enter the second number : 3
Enter the third number : 4
first > third > second
2–16. 文件
键入2.15节的文件显示的代码, 然后运行它, 看看能否在你的系统上正常工作,然后试
一下其它的输入文件。
【答】略