Python beginner's note (2): Arabic numerals are converted to Chinese uppercase,

Source: Internet
Author: User

Python beginner's note (2): Arabic numerals are converted to Chinese uppercase,

Question: enter a number and convert it to an uppercase Chinese character.

Executable program (Python 2.7.9 ):

1 #-*-coding: UTF-8-*-# write Chinese characters in the py file of python2. A line of comments indicating file encoding must be added, otherwise, python2 uses the ASCII encoding 2 dic_num = {"0": u "0", "1": u "one", "2": u "two ", "3": u "San", "4": u "Si", "5": u "Wu", "6": u "Lu", "7 ": u "weight", "8": u "pick", "9": u "weight"} 3 dic_unit = {0: u "", 1: u "pick ", 2: u "yellow", 3: u "yellow", 4: u "Ten Thousand"} 4 5 flag = True 6 7 while flag: # ensure that the program can run 8 shu = [] 9 big = ''10 num = raw_input (" enter a number (the value range is 1 ~ Between 99999). If q is entered, exit the program :". decode ('utf-8 '). encode ('gbk') 11 if num = 'q' or num = 'q': 12 flag = False13 elif int (num) <1 or int (num)> 99999: 14 print "error! Enter 1 ~ A number between 99999! \ N ". decode ('utf-8 '). encode ('gbk') 15 continue16 else: 17 listnum = list (num) 18 lennum = len (listnum)-119 for item in listnum: 20 shu. append (dic_num [item]) # first, add the Chinese characters corresponding to the first number in the input number to the shu list, and then loop 21 shu. append (dic_unit [lennum]) # For example, add the "digit" corresponding to "3" in dic_unit to the first number of shu in four digits, subsequent Cycle 22 lennum-= 123 big = ''. join (shu) 24 print big. encode ('gbk') 25 print "\ n"

Note: There are many incomplete examples of the preliminary programming method that I just learned, such as: 1. if you enter a number starting with "082" and a number starting with "0", the system will output "0, 2. if you enter "034354", an error is reported. No exception processing result is added.

We will continue to learn more in the future.

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.