Write a program that generates a color ball number, and the generated number is written to the file.
# The winning number consists of 6 red ball numbers and a blue ball number
# Basketball Range: 01-16
# Red ball Range: 01-33
def swq (num):
Random.ranint (1,16)
#tikti. txt
Basketball: XX red ball number is xx 01 08 09 12 13 19
Basketball: XX red ball number is xx 01 08 09 12 13 19
Import Random
F=Open"Tickets.txt","W+",Encoding="Utf-8")
DefSWQ (Num:
For IInchRangeNum:
BlueBall=STR (Random.randint (1,16))#随机产生篮球数字
IfLen (BlueBall)==1:
BlueBall=STR (blueball). Zfill (2)
#print (BlueBall)
Redball_set=Set ()
WhileLen (Redball_set)<6:
RedBall=STR (Random.randint (0,33))#随机产生红球数字
IfLen (RedBall)==1:
RedBall=STR (redball). Zfill (2)
#print (RedBall)
Redball_set.add (RedBall)
Redballs=list (redball_set)
Redball_str = "
for I in redballs:
redball_str =redball_ Str +i+ ""
F.write ( "basketball:" +blueball++redball_str+" \n ")
Swq (10)
The result after running is:
Basketball: 01 Red ball: 22 12 07 31 24 25
Basketball: 15 Red ball: 20 06 17 09 32 25
Basketball: 09 Red ball: 19 12 08 31 23 25
Basketball: 14 Red ball: 15 18 08 11 23 25
Basketball: 16 Red ball: 27 33 06 28 09 05
Basketball: 01 Red ball: 16 27 19 29 24 23
Basketball: 13 Red ball: 16 22 09 32 29 11
Basketball: 03 Red ball: 02 22 12 32 14 25
Basketball: 04 Red ball: 26 06 22 09 29 31
Basketball: 13 Red balls:
Summarize:
1. Because the number of red balls required to produce each time is 6 and cannot be duplicated, the resulting red ball can be put into the collection because the collection can be duplicated.
Redball_set.add (redball)
2. When printing, because it is necessary to print out the number of each line, but if the collection is directly converted to string printing, the red ball will be displayed as a series of single quotation marks, comma character channeling, not independent numbers, so you need to convert the set into a list, and then traverse out each value, into a string concatenation
For i in redballs:
Redball_str =redball_str +i+""
3. The resulting random number, if one number, requires the preceding 0 to become a double-digit, using the function "". Zfill ()
If len (blueball)= =1:
BlueBall = str (blueball). Zfill (2)
Python3-Write a program that generates a color ball number, and the generated number is written to the file