I'm working on a project for an Intro to C/Python class and am looking to improve the efficiency of the program. The program is a lottery simulation where the user inputs the number of tickets they want to buy, then generates tickets, and finally outputs the total winnings and net gain (usually loss).
This is my code (in Python, as required):
def main():
numb_tickets = int(input("How many tickets would you like to buy?\n"))
#Calculate Winnings
winnings = 0
for i in range(numb_tickets):
#For testing only, gives feedback progress of program
print(i," ",winnings)
#Creating winning ticket/your ticket, find number of matches
win_tic = getRandomTicket(MAX_VALUE, TIX_SIZE)
my_tic = getRandomTicket(MAX_VALUE, TIX_SIZE)
numb_win = numMatches(win_tic, my_tic)
#Add appropriate payout for number of matches
if numb_win == 6:
winnings += WIN_SIX
elif numb_win == 5:
winnings += WIN_FIVE
elif numb_win == 4:
winnings += WIN_FOUR
elif numb_win == 3:
winnings += WIN_THREE
#Calculate cost of purchasing tickets
cost_tics = numb_tickets * COST_TIC
if winnings >= cost_tics:
profit = winnings - cost_tics
print("You won $",winnings,", for a net earnings of $",profit,".", sep="")
elif winnings < cost_tics:
loss = cost_tics - winnings
print("You won $",winnings,", for a net loss of $",loss,".", sep="")
main()
Note: the getRandomTicket() and numMatches() functions were provided by the professor to generate a lottery ticket and check the number of matches it has, respectively.
My program works fine for smaller numbers of tickets, but when testing the required 1,000,000 tickets, takes a massive amount of time. It makes sense that the time increases rapidly as the range of the loop increases, but I don't know of a better way to loop this yet. Any input or suggestions are greatly appreciated.
print(i," ",winnings)
and see if the performance improves. Or you could print every 10000 byif i % 10000 == 0:
\$\endgroup\$print('You won ${}, for a net loss of ${}.'.format(winnings, loss))
. \$\endgroup\$