1.修改程序print_unicode.py,以便用户可以在命令行上输入几个单独的单词,并且只有在Unicode字符名包含用户所指定的所有单词时才打印相应列。
__author__ = 'Ibuki Suika'
import sys
import unicodedata
def print_unicode_table(words):
print("decimal hex chr {0:^40}".format("name"))
print("------- ----- --- {0:-<40}".format(""))
code = ord(" ")
end = min(0xD800, sys.maxunicode)
while code < end:
c = chr(code)
name = unicodedata.name(c, "*** unknown ***")
b = True
for word in words:
if word.lower() not in name.lower():
b = False
break
if b:
print("{0:7} {0:5X} {0:^3c} {1}".format(code, name.title()))
code += 1
words = []
if len(sys.argv) > 1:
if sys.argv[1] in ("-h", "--help"):
print("usage: {0} [string]".format(sys.argv[0]))
word = None
else:
words = sys.argv[1:]
if words is not None:
print_unicode_table(words)
2.修改quadratic.py,使得系数0.0对应的方程项不再输出,负数系数的输出形式为-n,而不是+-n。
__author__ = 'Ibuki Suika'
import cmath
import math
import sys
def get_float(msg, allow_zero):
x = None
while x is None:
try:
x = float(input(msg))
if not allow_zero and abs(x) < sys.float_info.epsilon:
print("zero is not allowed")
x = None
except ValueError as err:
print(err)
return x
print("ax\N{SUPERSCRIPT TWO} + bx + c = 0"