"Python Mini Practice" 0001

Source: Internet
Author: User

question No. 0001: as an independent developer of the Apple Store App, do you want to make a limited-time promotion, generate an Activation code (or coupon) for your app, and use Python to generate 200 activation codes (or coupons)?

#coding = Utf-8__author__='Liez'ImportRandomdefmake_number (num, length): Str='abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789'a=[] I=0 whileI <Num:numstr="'         forJinchRange (length): Numstr+=random.choice (str)ifNumstr not inchA:#if not repeateda.append (numstr) I+ = 1Print(NUMSTR) Make_number (20,10)

Note

random.choice(seq): Return a random element from the non-empty sequence seq. If seq is empty,

There are 20 of them, Sir:

COSSX9UMWF
qkmsbdvj0g
Ardycodyaf
F8H0DRSHJL
E0t6lf6ao1
ZB06XALSNF
SMCAQXYHQM
209ME2LSCL
NNVMMEMGQD
fqxlzvlzpc
dj7luhi53s
rytnv7yhth
Bg7pdfmge1
I7r7s1mpwk
Agn2pyf3ti
523YX6TDS8
Gmffcmyoyx
Mmzz2phieo
GAHTMOVPDT
6RGB3V2TJP

Ps:

1. I use the random module, but also see other people a lot of different practices. For example, the UUID module,uuid-universally unique identifier (universal unique identification code).

It's not clear what the difference between these sequence generation functions is (is it safer or something?). )。 UUID Module docs

2. I use list to put the generated serial number, also see with set : A set object is an unordered collection of distinct hashable objects. Common uses include membership testing, removing duplicates from a sequence, and computing mathematical, operations such as Intersection, Union, difference, and symmetric difference. More features. Set Types Docs

Specific changes:

defmake_number (num, length): Str='abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789'a= set ()//. .. i=0 whileI <Num:numstr="'         forJinchRange (length): Numstr+=random.choice (str)ifNumstr not incha:a|= {NUMSTR}//. .. i+ = 1Print(NUMSTR)

Exercise Books: Https://github.com/Yixiaohan/show-me-the-code

"Python Mini Practice" 0001

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.