习题1:题目:给一个不多于5位的正整数,要求:一、求它是几位数,二、逆序打印出各位数字。
#encoding=utf-8
while True:
try:
num=int(raw_input("input a number not more than 5 digits:"))
except:
"Plese input again:"
else:
if len(str(num))<=5:
break
print len(str(num))
print str(num)[::-1]
习题2:题目:请输入星期几的第一个字母来判断一下是星期几,如果第一个字母一样,则继续判断第二个字母。
#encoding=utf-8
week={'m':'monday','tu':'tuesday','w':'wednesday','th':'thursday','f':'friday','sa':'saturday','s':'sunday'}
prefix=raw_input("input the first letter:")
if week.has_key(prefix.lower()):
print week[prefix.lower()]
else:
prefix2=raw_input("input the second letter:")
if week.has_key(prefix.lower()+prefix2.lower()):
print week[prefix.lower()+prefix2.lower()]
习题3:两个 3 行 3 列的矩阵,实现其对应位置的数据相加,并返回一个新矩阵:
X = [[12,7,3],
[4 ,5,6],
[7 ,8,9]]
Y = [[5,8,1],
[6,7,3],
[4,5,9]]
#encoding=utf-8
x = [[12,7,3],
[4 ,5,6],
[7 ,8,9]]
y = [[5,8,1],
[6,7,3],
[4,5,9]]
z = [[0,0,0],
[0,0,0],
[0,0,0]]
#print x,y,z
for i in range(len(x)):
print "i:",i
for j in range(3):
print "j:",j
print "x[%s][%s]:%s"%(i,j,x[i][j])
print "y[%s][%s]:%s"%(i,j,y[i][j])
z[i][j]=x[i][j]+y[i][j]
print "z[%s][%s]:%s"%(i,j,z[i][j])
print z
c:Python27Scripts>python task_test.py
i: 0
j: 0
x[0][0]:12
y[0][0]:5
z[0][0]:17
j: 1
x[0][1]:7
y[0][1]:8
z[0][1]:15
j: 2
x[0][2]:3
y[0][2]:1
z[0][2]:4
i: 1
j: 0
x[1][0]:4
y[1][0]:6
z[1][0]:10
j: 1
x[1][1]:5
y[1][1]:7
z[1][1]:12
j: 2
x[1][2]:6
y[1][2]:3
z[1][2]:9
i: 2
j: 0
x[2][0]:7
y[2][0]:4
z[2][0]:11
j: 1
x[2][1]:8
y[2][1]:5
z[2][1]:13
j: 2
x[2][2]:9
y[2][2]:9
z[2][2]:18
[[17, 15, 4], [10, 12, 9], [11, 13, 18]]
一行搞定:
>>> [[x[i][j]+y[i][j] for i in range(3)] for j in range(3)]
[[17, 10, 11], [15, 12, 13], [4, 9, 18]]
Sys.argv练习把所有的输入参数的字母个数统计出来
#encoding=utf-8
import sys
print "The command line arguments are:"
list=sys.argv
num=0
print list
for i in list[1:]:
if (i>='a' and i<='z') or (i>='A' and i<='Z'):
num+=1
print num
c:Python27Scripts>python task_test.py A B c d
The command line arguments are:
['task_test.py', 'A', 'B', 'c', 'd']
4
第二道题:你输入的参数全部是数字,计算所有数字参数的累计和
#encoding=utf-8
import sys
print "The command line arguments are:"
list=sys.argv
sum=0
print list
for i in list[1:]:
sum+=float(i)
print "sum:",sum
c:Python27Scripts>python task_test.py 1 1 1.1
The command line arguments are:
['task_test.py', '1', '1', '1.1']
sum: 3.1
按照吴老思路:
#encoding=utf-8
import sys
if len(sys.argv[1:])>=1:
sum=0
else:
sum = None
for i in sys.argv[1:]:
try:
sum+=float(i)
except:
continue#这里的意思是出现非数字的地方不抱错,
print sum
#encoding=utf-8
import sys
import os
def readfile(filename):
f=open(filename)
while True:
line=f.readline()
if len(line.strip())==0:
break
print line
f.close()
print "sys.argv[0]--------",sys.argv[0]
print "sys.argv[1]--------",sys.argv[1]
print "sys.argv[2]--------",sys.argv[2]
if len(sys.argv)<2:
print "No action specified."
sys.exit()
if sys.argv[1].startswith('--'):
option = sys.argv[1][2:]
if option == 'version':
print "version 1.2"
elif option =='help':
print """
This program.........
--version:prints the version number
--help : display this help"""
else:
print "unknow option"
sys.exit()
else:
for file in sys.argv[1:]:
#print file
readfile(file)
sys.stdin.readline() == raw_input()
>>> raw_input()
sd
'sd'
>>> sys.stdin.readline()
sadf
'sadf '
>>>
#coding=utf-8
import os
import sys
counter=1
while True:
line=sys.stdin.readline()
if not line.strip():
break
print "%s:%s"%(counter,line)
counter +=1
>>> import sys
>>> sys.version
'2.7.14 (v2.7.14:84471935ed, Sep 16 2017, 20:25:58) [MSC v.1500 64 bit (AMD64)]'
>>>
>>> sys.stdout.write("sdfsdffffffffff ")
Sdfsdffffffffff
#coding=utf-8
import os
import sys
for i in range(3):
sys.stdout.write("gloryroad")
print ' ','_'*60,' '
for i in range(3):
sys.stderr.write("error error")
将标准输出到屏幕,改为输出到文件里
>>> import os
>>> import sys
>>> sys.stdout=open("d:\a.txt","w")
>>> print "love you"
>>> "love you"
>>> fp
>>> print "hello"
>>> stdout.flush()
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
NameError: name 'stdout' is not defined
>>> print ""
>>> sys.stdout.flush()