参考书籍:python程序设计
chapter5
1.
def main():
print("This pro converts the date.")
#input
date = input("Please input the date (mm/dd/yyyy):")
#split into components
month, day, year = date.split('/')
#convert month to monthname
monthname = ['January', 'February', 'March', 'April', 'May',
'June', 'July', 'August', 'September', 'October', 'November', 'December']
month = monthname[int(month) - 1]
#output
print("The converted date is {0} {1:0>2}, {2:0>4}".format(month, day, year))
main()
5.
def main():
print("This pro calculates a name's value.")
#get a name
Name = input("Enter a name:")
#calculate
name = Name.lower()
s = 0
for i in name:
num = ord(i)
num -= 96
s += num
print("The number of {0} is {1:4}".format(Name, s))
main()
6.
def main():
print("This pro calculates a name's value.")
#get a name
MultiName = input("Enter a name:")
#Split the name and calculate
NameList = MultiName.split()
s_all = 0
for Name in NameList:
name = Name.lower()
#each name's value
s = 0
for i in name:
num = ord(i)
num -= 96
s += num
s_all += s
print("The number of {0} is {1:4}".format(MultiName, s_all))
main()
7.
def main():
print("This pro simulates the Kaisa Code.")
#input
char = input('Enter the words(no space):')
key = int(input('Enter the key:'))
#move
new_char = ''
for ch in char:
ch = chr(ord(ch) + key)
new_char = new_char + ch
print("The words are {0}".format(new_char))
main()
8.
def main():
print("This pro simulates the real Kaisa Code.")
#input
char = input('Enter the words(space accetped):')
key = int(input('Enter the key(<5):'))
string = 'abcdefghigklmnopqrstuvwxyzabcdeABCDEFGHIGKLMNOPQRSTUVWXYZABCDE'
#split the char and convert it
charlist = char.split()
new_name = ''
for name in charlist:
# move one word
new_char = ''
for ch in name:
fir_index = string.find(ch)
sec_index = fir_index + key
new_char = new_char + string[sec_index]
new_name = new_name + new_char
print("The words are {0}".format(new_name))
main()
14.
def main():
print("This pro count the num of lines, words and chars for a txt.")
#open the txt file
score = open('./chapter5/score.txt')
#read the data
data = score.read()
#count
line_num = data.count('\n')
word_num = data.count(' ') + data.count('\n') #??
chars = len(data)
#close the file
score.close()
print("The txt file has {0} lines, {1} words, and {2} chars.".format(line_num, word_num, chars))
main()
15.
from graphics import *
def main():
print("This pro plot students' scores.")
#open the file
scorefile = open('./chapter5/score.txt', 'r')
#draw the win
win = GraphWin('Their Scores', 400, 300)
win.setBackground('white')
win.setCoords(-5, -5, 105, 75)
#get first line
num = int(scorefile.readline())
#get stus' names, their scores
names = []
scores = []
for line in scorefile.readlines():
line = line.split()
name = line[0] # a str
score = int(line[1]) # an int
names.append(name)
scores.append(score)
print(name)
#wait for click
win.getMouse()
#draw the names
for i in range(num):
name_text = Text(Point(len(names[i]) / 2 + 15, 20 * (num - i - 1) + 5), names[i])
name_text.setFill('black')
name_text.draw(win)
#wait for click
win.getMouse()
#draw the scores
for i in range(num):
score_rec = Rectangle(Point(40, 20 * (num - i - 1)), Point(scores[i], 20 * (num - i - 1) + 10))
score_rec.setOutline('black')
score_rec.setWidth(2)
score_rec.draw(win)
#click to quit
win.getMouse()
#close the win
win.close()
#close the file
scorefile.close()
main()