python习题练习(三)

参考书籍: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()

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值