I have made the two modules (which are incomplete I guess) for the main function but couldn’t make the main function
Create a script file named lottohelper_using_getopt.py for the module in your current workspace/directory.
Define a function named lotto649 (in the script/module file) which can generate and return six different random integers in a list from range [1,49]. (1 point)
Define a function named lottomax (in the script/module file) which can generate and return seven different random integers in a list from range [1,50]. (1 point)
Import the lottohelper_using_getopt module in Jupyter notebook, and call the two functions lotto649 and lottomax, and report your result in a Jupyter notbook cell. (1 point)
You have to use the randrange() or randint() function from the random module https://docs.python.org/3/library/random.html.
Define the main function (in the script/module file) which will allow you to provide options, through the getopt module, (3 point) such as
help information to show how to use it, choosing type between 649 and MAX, using -v or –verbose to control verbosity. You need to use a positional argument to specify how many tickets you want it to generate.
In the main function, once you parsed the parameters, you need to write code to generate multiple tickets. (1 point)
Run the lottohelper_using_getopt.py in command line (or in a Jupyter lab cell using !python lottohelper_using_getopt.py ), e.g. python lottohelper_using_getopt.py -v 1 –type MAX 4 which means you tell your script to generate 4 LottoMax tickets (i.e. four sets of random numbers printed on the screen). Report your result in a notebook Raw cell. (1 point)
This is the output that should come
# The running of your script (using getopt package should look like the following: $ python lottohelper_using_getopt.py -h lottohelper.py [-h|--help] [-v|--verbose] <0/1> [-t|--type] <649/MAX> <integer> $ python lottohelper_using_getopt.py -v 1 --t 649 3 Ticket Type: 649 Number of Tickets: 3 [24, 26, 27, 35, 39, 43] [12, 19, 20, 31, 44, 49] [2, 3, 11, 16, 22, 31] $ python lottohelper_using_getopt.py --verbose 0 --type MAX 3 [2, 11, 17, 18, 25, 32, 34] [10, 24, 25, 29, 31, 32, 39] [3, 4, 5, 6, 23, 43, 44]
lottohelper_using_getopt Module
#!/usr/bin/env python import random import getopt import sys def lotto649(): ''' Generate six different integers from range 1,2,...,49. ''' ticket=[] # the list to save the six numbersa while loop below to add the generated numbers to the list ticket, make sure not add same numbers in the ticket i=1 while i<=50: for x in range (6): ticket.append(x) # sort ticket ticket.sort() return ticket def lottomax(): ''' Generate seven different integers from range 1,2,...,50. ''' ticket=[] # the list to save the seven numbers # you will need to write a for loop below to add the generated numbers to the list ticket, make sure not add same numbers in the ticket x=[random.randint(1, 51) for i in range(7)] ticket.append(x) # sort ticket ticket.sort() # return your result below return ticket def usage(): print("lottohelper.py [-h|--help] [-v|--verbose] <0/1> [-t|--type] <649/MAX> <integer>") def main(): try: opts, args = getopt.getopt(sys.argv[1:], "h", ["help"]) # students: complete this statement except getopt.GetoptError as err: # print help information and exit: print(err) # will print something like "option -a not recognized" usage() sys.exit(2) # get optional arguments tp='649' # initialize type of ticket ver=False # verbose for k,v in opts: pass # get positional argument N=int(args[0]) # do actual work here # generate N tickets tickets=[] for n in range(N): pass if ver: print('Ticket Type:', tp) print('Number of Tickets:', N) for t in tickets: print(t) if __name__=='__main__': main()
I would be grateful if someone explains how to go about the question and solve it
Advertisement
Answer
This should do it. I added some comments for additional info, search them in your favourite engine if you don’t already know how they work.
import random, getopt, sys def make_ticket(length, maximum): """ Generate a ticket of a given length using numbers from [1, maximum] return random.sample(range(1, maximum+1), length) would be the best way """ ticket = [] while len(ticket) < length: num = random.randint(1, maximum) if not num in ticket: ticket.append(num) return ticket def usage(): print("lottohelper.py [-h|--help] [-v|--verbose] <0/1> [-t|--type] <649/MAX> <integer>") def main(args): try: opts, args = getopt.getopt(args, "ht:v:", "help type= verbose=".split()) # students: complete this statement except getopt.GetoptError as err: # print help information and exit: print(err) # will print something like "option -a not recognized" usage() sys.exit(2) # get optional arguments # this approach intentionally breaks if the user misses an option for k, v in opts: if k in '--help': usage() elif k in '--type': tp = { '649': (6, 49), 'MAX': (7, 50) }[v] # dict-based ternary operator elif k in '--verbose': ver = { '0': False, '1': True }[v] # dict-based ternary operator # get positional argument N = eval(args[0]) # causes an exception if anything other than an integer is given; a float will not be converted # do actual work here tickets = [make_ticket(*tp) for i in range(N)] # list comprehension if ver: print(f'Ticket Type:t{649 if tp[0]==6 else "MAX"}') # format string for i, t in enumerate(tickets, 1): print(f'Ticket #{i}:t{t}') #format string if __name__=='__main__': args = sys.argv[1:] # args = '-v 1 --t 649 3'.split() # main(args) # args = '--verbose 1 --type MAX 3'.split() main(args)