Program bit 001_python analog dot matrix digital

Source: Internet
Author: User

Tried a lot of programming languages, wrote a lot of programs (of course, are basically some of the gadgets to amuse themselves or give their own tools), and gradually realize that the program is a continuous improvement, continuous optimization of the process-programming first to have an idea (goal), around this goal to form the most basic functional logic, The test proves that the function is effective, even if the target is basically realized. Then, you can extend the application domain of basic functions, strengthen the fault tolerance and interactivity of the program, and gradually form a more perfect and robust program. Here's a small example of how this process can be shown:

In the calculator, the elevator, the LED display, you can see the numbers expressed in dot matrix

  

Here, we try to use Python to simulate dot-matrix numbers, in short, to magnify the numbers (^_^)

First, the design of the display of digital lattice effect

To simplify, we only consider 0~9 these 10 Arabic numerals, each digital lattice is 5 rows and 4 columns, in Python can be expressed in tuple. (in the process of designing lattice fonts, you can also experience the feeling of being an artist or font designer, @[email protected]). In Python, the process is simple (the following is an example of only 0, 12 numbers).  

fontcol=4fontrow=5zero= ("* *    ",          "*  *",          "*  *",          "*  *",          "* *") one= (     "  *",          " ** ",          "  * ",          "  * ",          " ***")

  

Second, the test output effect

In order to examine the output effect of digital lattice, and in order to verify the future output logic, we can use the simplest program to output all the fonts. The functional logic is simple:

A. For each number in a sequence, find its corresponding matrix, output the first line of the matrix, empty one, and then output the next number corresponding to the first line of the Matrix, the space, until all the numbers corresponding to the first line of the font has been output

B. Continue to output each line sequentially until the end (in this program is 5 lines, that is, Fontrow)

      Complete program:

import sysfontcol=4fontrow=5# build the Digital dot matrix zero= ("* *", "* *", "* *", " * * "," * * ") one= (" * "," * * "," * "," * "," * * ") two= (" * * "," * * "," * "  , "*", "* * *") three= ("* *", "* *", "* *", "* *", "* *") four= ("*", "* *" "," * * "," * * * "," * ") five= (" * * * "," * "," * * * * "," * "," * * * "," * * * ") six= (       "* *", "*", "* * *", "* *", "* *") seven= ("* * *", "* *", "*", "*", "* * *") eight= ("* *", "* *", "* *", "* *", "* *") nine= ("* *", "* *", "* *" , "*", "* *") digits= (zero,one,two,three,four,five,six,seven,eight,nine) #将所有数字字模合成一个元组i =0while I<fontro W:col=0 while col<10: #此处输出全部字模, a total of 10 print (digits[col][i],end= ") col+=1 print (" ") #换行, output matrix next Row I+=1 

Output Effect:

  

iii. optimization and refinement 1--Displays different outputs depending on the input

read input parameters via argv (that is, the specific number the user wants to display), if the user provide usage instructions and sample output if no parameters are supplied

Here you can use the above test output code to make a slight adjustment to form a function show_numbers. (Note that the print section has minor adjustments)

Core code:  

Import sysfontcol=4fontrow=5# build the digital dot matrix # digits= (zero,one,two,three,four,five,six,seven,eight,nine) # Synthesize all digital fonts into a tuple def show_number (nums): i=0 while    i<fontrow: col=0 while        Col<len (nums): # Here output all font            print (Digits[int (Nums[col])][i],end= "")            col+=1        print ("") #换行, output matrix next line        i+=1def show_usage ():    print ("Please provide an integer within 15 digits after the program name")    print ("program name. PY 0123456789")    show_number (' 0123456789 ') If Len ( SYS.ARGV) ==2:    show_number (sys.argv[1]) Else:    show_usage () #如果未提供参数或提供的参数过多, the output usage instructions

 

Iv. optimization and Improvement 2--Check the input

The above code is already able to cope with the basic requirements, but there is no resistance to the user's error input. We plan to optimize from the following areas:

A. Limiting the maximum number of input characters for a user

    On the DOS interface, a line displays up to 80 characters, each of which is 4 characters, the font has 1 characters spaces, and the newline character is considered at the end of line, so we allow up to 80/(4+1) -1=15 numbers at a time.

B. Restrict user input to Arabic numerals only

    If the user mistakenly input non-numeric information, such as English characters, etc., then the Int () function will report ValueError, catch this error and hint

the program after adjustment:

Try:    if Len (sys.argv) ==2:        If Len (sys.argv[1]) <16:            show_number (sys.argv[1])        else:            show_ Usage ()    else:        show_usage () #如果未提供参数或提供的参数过多, the output usage description except ValueError:    print ("\ n \ nthe all information is not displayed correctly!!) -A non-numeric character appears! \ n ")    Show_usage ()

Program bit 001_python analog dot matrix digital

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.