要求
编写一个程序,当输入python bigdigists.py 234234,会输出下列结果。
源码
import sys
Zero = [" *** ",
" * * ",
"* *",
"* *",
"* *",
" * * ",
" *** "]
#把下面的list竖起来就跟这个zero一个样
One = [" * ", "** ", " * ", " * ", " * ", " * ", "***"]
Two = [" *** ", "* *", "* * ", " * ", " * ", "* ", "*****"]
Three = [" *** ", "* *", " *", " ** ", " *", "* *", " *** "]
Four = [" * ", " ** ", " * * ", "* * ", "******", " * ",
" * "]
Five = ["*****", "* ", "* ", " *** ", " *", "* *", " *** "]
Six = [" *** ", "* ", "* ", "**** ", "* *", "* *", " *** "]
Seven = ["*****", " *", " * ", " * ", " * ", "* ", "* "]
Eight = [" *** ", "* *", "* *", " *** ", "* *", "* *", " *** "]
Nine = [" ****", "* *", "* *", " ****", " *", " *", " *"]
Digits = [Zero, One, Two, Three, Four, Five, Six, Seven, Eight, Nine]
try:
digits = sys.argv[1] #读取用户输入命令
row = 0
while row < 7:
line = ""
column = 0
while column < len(digits): #len("aadfs")作用是确定字符串“aadfs”的长度
number = int(digits[column])
digit = Digits[number]
line += digit[row] + " " #通过使用加好合并字符串
column += 1
print(line)
row += 1
except IndexError: #如果用户不输入命令,就会报出IndexError错误
print("usage: bigdigits.py <number>")
except ValueError as err:
print(err, "in", digits)
出现的问题及用到的知识点
1.sys.argv用法
import sys输入python test.py arg1 arg2 arg3
print(sys.argv[0])
print(sys.argv[1])
print(sys.argv[2])
print(sys.argv[3])
输出
test.py
arg1
arg2
arg3
欢迎收听我的微信公众号