Program bit by bit 00w.python simulates dot matrix numbers, 00w.python Dot Matrix

Source: Internet
Author: User
Tags arabic numbers

Program bit by bit 00w.python simulates dot matrix numbers, 00w.python Dot Matrix

I have tried many programming languages and written many programs (of course, they are basically gadgets that entertain myself or use tools for myself ), gradually Realize that programming is a process of continuous improvement and Optimization-first of all, programming should have an idea (GOAL) to form the most basic functional logic around this goal, after the test proves that the function is effective, the goal is basically achieved. Subsequently, it can expand the application fields of basic functions, enhance the fault tolerance and interoperability of programs, and gradually form a more complete and robust program. Here is a small example to illustrate this process:

On the calculator, on the elevator, and on the LED display screen, you can see numbers represented by dots.

  

Here, we try to use Python to simulate dot matrix numbers. In short, it is to enlarge the numbers (^_^)

I. design the Display Effect of dot matrix numbers

For simplicity, we only consider 0 ~ 9 each of the ten Arabic numbers has five rows and four columns. in Python, tuple can be used for representation. (You can also experience the feeling of being an artist or font designer during the design of the dot matrix model ,@_@). In Python, this process is very simple (the following uses 0, 1 numbers as an example ).

FONTCOL=4FONTROW=5zero=(    " ** ",          "*  *",          "*  *",          "*  *",          " ** ")one=(     "  * ",          " ** ",          "  * ",          "  * ",          " ***")

  

Ii. Test the output result

To evaluate the output results of the digital dot matrix and verify the future output logic, you can use a simple program to output all the fonts. The function logic is simple:

A. for each number in a sequence, find its corresponding modulo and output the first line of the modulo, with an empty grid. Then, output the first line of the modulo corresponding to the next number, with a space, until all the numbers correspond to the first line of the modulo.

B. output each row in sequence until the end (five rows in this program, namely FONTROW)

Complete program:

Import sysFONTCOL = 4 FONTROW = 5 # Build the following digital dot matrix zero = ("**","**","**","**","**") one = ("*", "**", "*", "*", "***") two = ("**","**", "*", "*", "*****") three = ("**","**","**","**", "**") four = ("*","**","**","****","*") five = ("*****", "*", "*****", "*", "*****") six = ("**", "*", "***") seven = ("***","**","*", "*", "***") eight = ("**","**","**","**","**") nine = ("**", "**", "***", "*", "**") digits = (zero, one, two, three, four, five, six, seven, eight, nine) # synthesize all numeric fonts into a single tuple I = 0 while I <FONTROW: col = 0 while col <10: # All fonts are output here. A total of 10 print (digits [col] [I], end = "") col + = 1 print ("") # line feed, output mod next line I + = 1

Output result:

  

 3. Optimization and Improvement 1-- Display different outputs based on different inputs

Read the input parameters through argv (that is, the specific number that the user wants to display). If the user does not provide the parameters, the usage instructions and sample output are provided.

Here we can slightly adjust the test output code used above to form a function show_numbers. (Note that the print part is slightly adjusted)

Core code:

Import sysFONTCOL = 4 FONTROW = 5 # Build a digital dot matrix below # Slightly digits = (zero, one, two, three, four, five, six, seven, eight, nine) # merge all numeric modulo into a single tuples def show_number (nums): I = 0 while I <FONTROW: col = 0 while col <len (nums ): # print (digits [int (nums [col])] [I], end = "") col + = 1 print ("") # line feed, output Model next line I + = 1def show_usage (): print ("please provide an integer of less than 15 digits after the program name") print ("program name. py 0123456789 ") show_number ('123') if len (sys. argv) = 2: show_number (sys. argv [1]) else: show_usage () # If no parameter is provided or too many parameters are provided, the usage instructions are output.

Iv. Optimization and Improvement 2-- Check input

The above code can meet basic requirements, but it has no resistance to incorrect input. We plan to optimize the following aspects:

A. Limit the maximum number of characters entered by a user

On the DOS interface, a single line can display a maximum of 80 characters, each of which occupies 4 characters and has a 1-character space. line breaks at the end of the line are considered, therefore, we can enter up to 80/(4 + 1)-1 = 15 numbers at a time.

B. restrict users from entering only Arabic numbers

If the user mistakenly enters non-numeric information, such as English characters, the int () function will report ValueError, capture the Error and prompt

Adjusted program:

Try: if len (sys. argv) = 2: if len (sys. argv [1]) <16: show_number (sys. argv [1]) else: show_usage () # If no parameter is provided or too many parameters are provided, the following output uses the parameter t ValueError: print ("\ n failed to display all information correctly !! -- A non-numeric character is displayed! \ N ") show_usage ()

Related Article

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.