Python核心编程第2版第六章习题答案

6-2 字符串标识符。修改例 6-1 的 idcheck.py 脚本,使之可以检测长度为一的标识符,并且可以识别Python关键字,对后一个要求,你可以使用keyword模块(特别是 keyword.kelist)来辅助

import string
import keyword
import sys

Startwith=string.ascii_letters+'_'
Othersymbol=string.digits


def CheckID(s):
    if s[0] in Startwith:
        if len(s)==1:
            print("The ID is valid")
            if s in keyword.kwlist:
                print ("And the ID is a key word")
        else:
            for j in s[1:]:
                if j not in Othersymbol+Startwith:
                    print ("The other symbol is invalid.")
#                    sys.exit()
                    break
            print ("The ID is valid.")
            if s in keyword.kwlist:
                print ("And the ID is a key word")
    else:
        print ("The startwith is invalid.")


if __name__=="__main__":
    sid=input("Enter a string:")
    CheckID(sid)

6-3 排序
(a) 输入一串数字,从大到小排列之。
(b) 跟 a 一样,不过要用字典序从大到小排列之.。
(a)

num1 = []
num = input("输入一串数字:") 
for i in num:
    num1.append(i) 
    #  num1.append(int(i))

num1.sort()  #若删掉该行则是以cghd输入,dhgc输出
num1.reverse() 
print (num1)

6-8 列表, 给出一个整型值,返回代表该值的英文, 比如输入89返回”eight-nine”. 附件题: 能够返回符合英文语法规则的形式, 比如输入”89” 返回”eighty-nine”. 本练习中的值限定在0~1000.

#coding=utf-8
units = [
'zero','one','two','three','four','five','six','seven','eight','nine','ten',
'eleven','twelve','thirteen','fourteen','fifteen','sixteen','seventeen',
'eighteen','nineteen'
]
tens = ['twenty','thirty','forty','fifty','sixty','seventy','eighty','ninety']


while True:
    myInput = input("Enter the num -->(input 'quit' to quit):")       #字符串输入
    if myInput == 'quit':
        break
    myInput_num = int(myInput)                      #将字符串输入转整型
    Input_num = []                                  #列表

    for i in myInput:                   #将输入的字符串转化为列表
        Input_num.append(int(i))

    if myInput_num < 20:                  #20以下的英文输出
        print (units[myInput_num])

    elif myInput_num < 100:             #20-100的输出
        if Input_num[1] == 0:           # 如50,第2位为0
            print (tens[Input_num[0] - 2])
        else:                           # 如53
            print (tens[Input_num[0] - 2] + '-' + units[Input_num[1]])

    elif myInput_num < 1000:     #100-1000
        if (Input_num[2] == 0 and Input_num[1] == 0):     #第二、三位等于0(注:此语句需要放在最前,如果后两句先执行的话,会包含这种情况,最后一位会输出zero)
            print (units[Input_num[0]] + ' ' + 'hundred')
        elif Input_num[1] == 0:       #第二位等于0
            print (units[Input_num[0]] + ' ' + 'hundred' + ' '+ 'and' + ' ' + units[Input_num[2]])
        elif Input_num[2] == 0:     #第三位等于0
            print (units[Input_num[0]] + ' ' + 'hundred' + ' '+ 'and' + ' ' + tens[Input_num[1] - 2] )
        else:                       #每一位都不等于0
            print (units[Input_num[0]] + ' ' + 'hundred' + ' '+ 'and' + ' ' + tens[Input_num[1] - 2] + '-' + units[Input_num[2]])

Enter the num -->(input 'quit' to quit):78
seventy-eight

Enter the num -->(input 'quit' to quit):5
five

Enter the num -->(input 'quit' to quit):2233

Enter the num -->(input 'quit' to quit):

6-10 字符串。写一个函数,返回一个跟输入字符串相似的字符串,要求字符串的大小写翻转。比如,输入“Mr.Ed”,应该返回“mR.eD”作为输出。

def myChang(myString): 
    return myString.swapcase() 

myString = input("Enter a String: ") 
print (myChang(myString))
或
str = input()     
print (str.swapcase())

猜你喜欢

转载自blog.csdn.net/qq_32482091/article/details/82424111