快乐家人

  博客园 :: 首页 :: 博问 :: 闪存 :: 新随笔 :: 联系 :: 订阅 订阅 :: 管理 ::
#2-7
s = input("请输入字符串:")
for i in s:
print(i)
i = 0
slen = len(s)
while i < slen:
print(s[i])
i += 1



#2-8
list1 = [1,2,3,4,6]
tuple1 = (1,2,3,4,6)
#方法1
i = 0
num = 0
llen = len(tuple1)
while i < llen:
num += list1[i]
i+=1
#方法2
for i in tuple1:
num += i

print(num/5)

#2-9
list1 = [1,2,3,4,6]
tuple1 = (1,2,3,4,6)
#方法1
i = 0
num = 0
llen = len(tuple1)
while i < llen:
num += list1[i]
i+=1
#方法2
for i in range(5):
num += int(input("请输入数值:"))

print(float(num/5))

#2-10
while 1:
num = int(input("请输入一个1和100之间的数:"))
if num >=1 and num <=100:
print("输入正确")
break
else:
print("输入错误,请重新输入")

#2-12
>>> dir()

['__builtins__', '__doc__', '__name__', '__package__']

dir() 函数不带参数时,返回当前范围内的变量、方法和定义的类型列表;带参数时,返回参数的属性、方法列表。

(b).不加括号输入dir:

>>> dir

<built-in function dir>
我认为是建立一个dir内置函数

(c).>>> type(dir)

<type 'builtin_function_or_method'>

type()内建函数接受任意数量的python对象作为参数并返回他们的类型

#2-13
(a).>>> dir()  #返回当前范围内的变量和方法 定义的类型列表
['__builtins__', '__doc__', '__name__', '__package__']
>>> import sys
>>> dir()
['__builtins__', '__doc__', '__name__', '__package__', 'sys']
>>> dir(sys) #带sys参数 返回sys的属性和方法列表
['__displayhook__', '__doc__', '__excepthook__', '__name__', '__package__', '__stderr__', '__stdin__', '__stdout__', '_clear_type_cache', '_current_frames', '_getframe', '_mercurial', 'api_version', 'argv', 'builtin_module_names', 'byteorder', 'call_tracing', 'callstats', 'copyright', 'displayhook', 'dllhandle', 'dont_write_bytecode', 'exc_clear', 'exc_info', 'exc_type', 'excepthook', 'exec_prefix', 'executable', 'exit', 'flags', 'float_info', 'float_repr_style', 'getcheckinterval', 'getdefaultencoding', 'getfilesystemencoding', 'getprofile', 'getrecursionlimit', 'getrefcount', 'getsizeof', 'gettrace', 'getwindowsversion', 'hexversion', 'long_info', 'maxint', 'maxsize', 'maxunicode', 'meta_path', 'modules', 'path', 'path_hooks', 'path_importer_cache', 'platform', 'prefix', 'ps1', 'ps2', 'py3kwarning', 'setcheckinterval', 'setprofile', 'setrecursionlimit', 'settrace', 'stderr', 'stdin', 'stdout', 'subversion', 'version', 'version_info', 'warnoptions', 'winver']

(b).显示sys模块的版本号属性及平台变量。记住在属性名前一定要加sys.,这表示这个属性是sys模块的。其中version变量保存着你使用的python解释器版本,platform属性则包含你运行python时使用的计算机平台信息

(c).最后,调用sys.exit()函数。这是一种热键之外的另一种退出python解释器的方式。


#2-15

(a)
first = input('Input the first number: ')
second = input('Input the second number: ')
third = input('Input the third number: ')
tmp = 0
if first > second:
tmp = first
first = second
second = tmp
if first > third:
tmp = first
first = third
third = tmp
if second > third:
tmp = second
second = third
third = tmp

print(first, second, third)

(b)
first = input('Input the first number: ')
second = input('Input the second number: ')
third = input('Input the third number: ')
tmp = 0
if first < second:
tmp = first
first = second
second = tmp
if first < third:
tmp = first
first = third
third = tmp
if second < third:
tmp = second
second = third
third = tmp

print(first, second, third)
 







posted on 2022-01-02 14:35  快乐家人  阅读(74)  评论(1编辑  收藏  举报