need:
Two-color ball, the generated number is written to the file
The winning number consists of 6 red ball numbers and 1 blue ball number
Basketball range: 01-16, random.ranint(1,16), 1-9 to be zeroed
Red Ball Range: 01-33
def ssq(num): #How many numbers are selected by random machine, complete with function
Output non-repeated number files (different order of red balls counts as duplicates)
Output format blue ball: 05 red ball 03 33 02 13 14 19
accomplish:
import random def auto_select(): #Randomly select a two-color ball result blue = str(random.randint(1, 17)).zfill(2 ) red_s = set() while len(red_s) != 6: #Do not use random.sample() because random N elements may be repeated red = str(random.randint(1, 34)).zfill(2 ) red_s.add(red) res = ' Blue ball: {} Red ball: {}\n ' .format(blue, ' ' .join(sorted(red_s))) #Different order of red balls counts as duplicates, so add sorting return res def write_file(l): #Write the machine selection result to a file with open( ' Double -color ball machine selection result ' , ' w ' , encoding= ' utf-8 ' ) as fw: fw.writelines(l) def main(): num = input( ' Please input the number to be selected by machine ' ) if num.isdigit(): num = int(num) res_s = set() while len(res_s) != num: res_s.add(auto_select()) write_file(res_s) else : print ( ' The number of machine selections must be an integer ' ) main()