2020_2_13_Study_Note_Pad

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.

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值