Check out my new e-store: www.davesabode.tictail.com
Python Example Code The example below is of a lucky number picker for lottery games here in the UK. It doesn't just pick random numbers! It actually produces random numbers until the same number is drawn four times in a row (that's a lot of random numbers). It's a nice way of adding a bit of luck to the selected numbers.
I've since edited the below code to request number that you'd like to omit. Whilst this is more functional, it's very much a work in progress. The new version asks you to enter numbers to omit and type 'end' when you're finished. One problem is that if there aren't enough numbers left to draw, an infinite loop will be created while the program waits for the draw to complete. It's easily fixed I guess, but it's starting to look untidy. I'm looking into learning Java so that I can work towards making mobile app where I might be able to present things better and even share it. This page is currently pretty popular. Get in touch if you have any recommendations or you'd like something further explaining via [email protected] . |
"""Currently:
Asks which game to pick numbers for. Currently supported: Lottery,Euromillions,Thunderball Asks how many draws. Draws the right amount of balls to the right values (including thunderball and lucky stars) for each draw Checks for matching balls in each draw and corrects them (using a while loop). Only draws a number when it has randomly been selected three times consecutively(lucky!) Future: Include hotpicks. Include a custom game where rules are required (highest ball, number of balls, bonus balls etc.) """ import random, os def cls(): os.system(['clear','cls'][os.name == 'nt']) def chooseGame(): #chooses a game for number picking print "(L)ottery","(E)uroMillions","(T)hunderball" game=raw_input("Which of the above games would you like me to pick numbers for? ").lower() if game=="l" or game=="lottery" or game=="lotto" or game=="lot" or game=="lo": print "Lottery" lottopicker() elif game=="e" or game=="euro" or game=="euromillions" or game=="eu" or game=="eur": print "EuroMillions" euro() elif game=="t" or game=="thunder" or game=="thunderball" or game=="th" or game=="thu" or game=="thun" or game=="thund": print "Thunderball" thunder() else: print "Game not currently supported. Please select another game." chooseGame() def getRandom(x): luck=0 luck2=1 luck3=2 rnd=3 while luck!=luck2 or luck!=rnd or luck!=luck3: luck=random.randint(1,x) luck2=random.randint(1,x) luck3=random.randint(1,x) rnd=random.randint(1,x) return rnd def rerun(): runAgain=raw_input("Would you like to pick numbers for other games?").lower() if runAgain=="y" or runAgain=="yes": chooseGame() else: print "Thank you for using Lottery Number Picker. Good Luck!" def lottopicker(): #lottery number picker cls() print "Lottery Number Picker" draws=int(raw_input("How many sets of numbers would you like me to pick?")) print "Thank You" for x in range(draws): print "draw",x+1 #Main balls high=49 ball1=getRandom(high) ball2=getRandom(high) ball3=getRandom(high) ball4=getRandom(high) ball5=getRandom(high) ball6=getRandom(high) #Make sure none of the main balls are the same while ball1==ball2: ball2=getRandom(high) while ball1==ball3 or ball2==ball3: ball3=getRandom(high) while ball1==ball4 or ball2==ball4 or ball3==ball4: ball4=getRandom(high) while ball1==ball5 or ball2==ball5 or ball3==ball5 or ball4==ball5: ball5=getRandom(high) while ball1==ball6 or ball2==ball6 or ball3==ball6 or ball4==ball6 or ball5==ball6: ball6=getRandom(high) #Display results print "("+str(ball1)+")","("+str(ball2)+")","("+str(ball3)+")","("+str(ball4)+")","("+str(ball5)+")","("+str(ball6)+")" rerun() def euro(): #Euromillions number picker cls() print "EuroMillions Number Picker" draws=int(raw_input("How many sets of numbers would you like me to pick?")) print "Thank You" for x in range(draws): print "draw",x+1 #Main balls high=50 ball1=getRandom(high) ball2=getRandom(high) ball3=getRandom(high) ball4=getRandom(high) ball5=getRandom(high) #Make sure none of the main balls are the same while ball1==ball2: ball2=getRandom(high) while ball1==ball3 or ball2==ball3: ball3=getRandom(high) while ball1==ball4 or ball2==ball4 or ball3==ball4: ball4=getRandom(high) while ball1==ball5 or ball2==ball5 or ball3==ball5 or ball4==ball5: ball5=getRandom(high) #Lucky Stars lsHigh=11 ballLS1=getRandom(lsHigh) ballLS2=getRandom(lsHigh) #Make sure the lucky stars don't match while ballLS1==ballLS2: ballLS2=getRandom(lsHigh) print "("+str(ball1)+")","("+str(ball2)+")","("+str(ball3)+")","("+str(ball4)+")","("+str(ball5)+")","<>","("+str(ballLS1)+")","("+str(ballLS2)+")" rerun() def thunder(): #Thunderball number picker cls() print "Thunderball Number Picker" draws=int(raw_input("How many sets of numbers would you like me to pick?")) print "Thank You" for x in range(draws): print "draw",x+1 #Main balls high=39 ball1=getRandom(high) ball2=getRandom(high) ball3=getRandom(high) ball4=getRandom(high) ball5=getRandom(high) #Make sure none of the main balls are the same while ball1==ball2: ball2=getRandom(high) while ball1==ball3 or ball2==ball3: ball3=getRandom(high) while ball1==ball4 or ball2==ball4 or ball3==ball4: ball4=getRandom(high) while ball1==ball5 or ball2==ball5 or ball3==ball5 or ball4==ball5: ball5=getRandom(high) #Thunderball thunderHigh=14 thunder=getRandom(thunderHigh) print "("+str(ball1)+")","("+str(ball2)+")","("+str(ball3)+")","("+str(ball4)+")","("+str(ball5)+")","<>","("+str(thunder)+")" rerun() cls() chooseGame() |
Comments Imported extra commands. Without this line, some commands wouldn't work. This cls() function has been borrowed from an online forum to clear the screen. I'm not sure how it works. This ' chooseGame() ' function asks the user what game they'd like to pick numbers for. Takes an input and saves it to game as a lowercase string (easier to check) Checks to see if game is equal to l, lottery, lotto, lot, or lo If it is it prints "Lottery" and calls the lottopicker() function. Checks to see if game is equal to euro or similar If it is prints "Euromillions" and calls the euro() function Checks to see if game is equal to thunderball or similar If it is prints "Thunderball" and calls the thunder() function If the input is not recognised, a message is displayed and the chooseGame() function is run again. This getRandom(x) function give us the luck spin. First of all, it takes in a number. This number (x) is the highest a generated number is allowed to be. By taking in this number, this function can be used to generated a larger range of numbers. Four names (luck, luck2, luck3 and rnd) are assigned different values. While they are different, each one is given a random value (up to the value of x). If they're not all the same (by unlikely chance), the process repeats. Once the rare event occurs, the random number is returned. This rerun() function gives the user the chance to run the application again (for a different game if they like) The lottopicker() function picks out six lottery numbers (each up to the value of 49) using the getRandom() function. It asks how many sets of numbers to pick and saves that number as draws. This for loop will run once for each requested set of numbers (draw). Here we print the draw number. It adds one to it because for starts from 0. high is set to 49 and used for the balls below. doing it like this means I can easily copy and paste code for similar types of draw. These while loops only run if two balls are the same. If they are, the later ball picked is replaced with a new random number using the getRandom() function. This is where the results are displayed. "(" and ")" are added to better present the information. the str() command displays the information within the brackets as a string rather than as a number. This looks the same when the program is run, but it helps Python understand what you want it to do. for example, ball1 could be 2. Now, when you tell Python to print "("+2 it doesn't make sense because you can't add 2 to "("! However, if you print "("+"2" it knows that you want to join them rather than add them. By using str(ball1) your printing "2" instead of 2. Hope I didn't lose you there. This time High is set to 50 and there are only 5 main balls. The same getRandom() function is used. Again, we make sure no two balls are the same Now we have to add two mall balls which, in the game are drawn separately. These 'Lucky Stars' go up to 11. As we've made the getRandom() function flexible, it's not a problem to select those using it, Of course, we don't want our two Lucky Stars to be the same. Calls the rerun() function so that we can start again. Same as above but now we're only looking at numbers up to 39 And here we make sure that none of the numbers match. And to pick our single Tunderball... The rerun() function is called to give the option of rerunning the application. This is the first calling of a function, it clears the screen. This is where it all begins. The calling of the chooseGame() function |
Copy the code on the left hand side, save and run it via Python's IDLE. If it doesn't help you learn anything, at least it might make you a millionaire!
I'd love to see and questions you have on this as well as how you'd improve it. It'd be nice to see some of your examples of code which I could even share here. Just get in touch via my Contact page.
I'd love to see and questions you have on this as well as how you'd improve it. It'd be nice to see some of your examples of code which I could even share here. Just get in touch via my Contact page.