I have just finished the first chapter of the book called Programming in Python 3 Second Edition yesterday, but the exercises were completed only one.
The first one is an interesting program, which could make the digits that input to the computer huge and they will be made of “*”. The principle is not so hard, it relates to the list, I mean, each number corresponds to a huge digit. More importantly, the output will be printed row by row. And you can glance over the code.
Zero=[" **** ",
" * * ",
" * * ",
" * * ",
" * * ",
" * * ",
" **** ",]
One=[ " * ",
" * * ",
" * ",
" * ",
" * ",
" * ",
" ***** ",]
Two=[ " ***** ",
" * * ",
" * * ",
" * ",
" * ",
" * ",
" ******* ",
]
Three=[" **** ",
" * * ",
" * ",
" *** ",
" * ",
" * * ",
" **** ",]
Four=[" * ",
" ** ",
" * * ",
" * * ",
"******* ",
" * ",
" * ",]
Five=[" **** ",
" * ",
" * ",
" **** ",
" * ",
" * ",
" **** ",]
Six=[ " **** ",
" * ",
" * ",
" * ** * ",
" * * ",
" * * ",
" * ** * ",]
Seven=[" *******",
" *",
" * ",
" * ",
" * ",
" * ",
" * ",]
Eight=[" **** ",
" * * ",
" * * ",
" **** ",
" * * ",
" * * ",
" **** ",]
Nine=[ " *******",
"* *",
"* *",
" *******",
" *",
" *",
" *",]
Digits=[Zero,One,Two,Three,Four,Five,Six,Seven,Eight,Nine]
def bigdigts(digit,Digits=Digits):
#Digits was assigned by Digits outside the function
#Moreover, nums = Digits is also can be accepted.
row = 0
if len(digit)==0:
print("Error")
else:
while row<7:
line="" #Each row we need to initialize the line.
for i in digit:
line += Digits[i][row] + " "
print(line)
row+=1
bigdigts([0,1,2,3,4,5,6,7,8,9])
This code I refer to the
Python 字符串练习 ——“大数字”https://blog.csdn.net/Jason__Yeung/article/details/78586484?Utm_source = blogxgwz6
And this is the time to talk about the second one, also the last one.
What I have written down is pretty simple, and I just can not realize the function that only repeat the current line if there was an error. You can check it.
import random
#random.random() feilds float num in domain [0,1)
#random.randint(a,b) feilds int num in domain [a,b]
while True:
try:
rows = int(input("rows: "))
cols = int(input("columns: "))
minimum = input("minimum(or Enter for 0): ")
maximum = input("maximum(or Enter for 1000): ")
if minimum == "":
minimum = 0
else:
minimum = int(minimum)
if maximum == "":
maximum = 1000
else:
maximum = int(maximum)
for i in range(cols):
line = ""
for j in range(rows):
line += str(random.randint(minimum,maximum)) + " "
print(line)
except ValueError as err:
print(err)
And look at the expert’s
import random
def get_int(msg, minimum, default):
while True:
try:
line = input(msg)
if not line and default is not None:
return default
i = int(line)
if i < minimum:
print("must be >=", minimum)
else:
return i
except ValueError as err:
print(err)
rows = get_int("rows: ", 1, None)
columns = get_int("columns: ", 1, None)
minimum = get_int("minimum (or Enter for 0): ", -1000000, 0)
default = 1000
if default < minimum:
default = 2 * minimum
maximum = get_int("maximum (or Enter for " + str(default) + "): ",
minimum, default)
row = 0
while row < rows:
line = ""
column = 0
while column < columns:
i = random.randint(minimum, maximum)
s = str(i)
while len(s) < 10:
s = " " + s #Make each num's long is 10(NEAT)
line += s
column += 1
print(line)
row += 1
I am not so good at function definition, because most of time when I need to use multiple statements with similar styles, I forget to define a function and I just copy and paste them.