|
[Python] Powerball number generator
|
|
03-20-2010, 09:39 PM
(This post was last modified: 03-22-2010 01:44 AM by Alphablend.)
Post: #1
|
|||
|
|||
|
[Python] Powerball number generator
http://openbookproject.net/pybiblio/prac...erball.php
Powerball number generator Description To win the Powerball lottery (an extremely unlikely event so don't waste your time) you have to pick six numbers correctly. The first five numbers are drawn from a drum containing 53 balls and the sixth is drawn from a drum containing 42 balls. The chances of doing this are 1 in 120,526,770. Write a program to generate a set of Powerball numbers by utilizing the choice function in Python's random module. Input Ask the user how many sets of Powerball numbers he or she would like. Output The program will print each set of Powerball numbers in numeric order. Sample session Official (but fruitless) Powerball number generator How many sets of numbers? 3 Your numbers: 3 12 14 26 47 Powerball: 2 Your numbers: 1 4 31 34 51 Powerball: 17 Your numbers: 10 12 49 50 53 Powerball: 35 Code: from random import randintFew notes: After you choose how many sets of numbers you want, the first 'while' loop gives some random numbers to our powerball list. After that, the 2 'for' loops check if we have double numbers in our list. Because after we draw one ball, that one cannot be chosen again and it is out of the game. Then the process repeats for the rest of the sets. If you have any questions or suggestions how to improve the code write it down.
![]()
|
|||
|
03-20-2010, 10:03 PM
Post: #2
|
|||
|
|||
|
RE: [Python] Powerball number generator
Very good task i can say. Another probability task !
And good use of bubble sort. One suggestion, length of that array is always (1..5), so in that bubblesort() function you don't need to use len() function, you can just put 5 already. Everything is just okay ! There's a fine line between genius and insanity. I have erased this line. Oscar Levant There's a fine line between an administrator and black hat hacker. I have erased this line. Dr DEBCOL |
|||
|
03-20-2010, 10:59 PM
Post: #3
|
|||
|
|||
|
RE: [Python] Powerball number generator
Well, I made this BubbleSort function before, and I've just copied it. However, later in the program I could have just used number 5. But my habit now is to always call the function len(list) because don't always know the length of list.
![]()
|
|||
|
03-21-2010, 01:18 PM
(This post was last modified: 03-21-2010 01:18 PM by codecaine.)
Post: #4
|
|||
|
|||
|
RE: [Python] Powerball number generator
You could also use no loops and randomize a list
. For example,PHP Code: import random; |
|||
|
03-21-2010, 07:59 PM
Post: #5
|
|||
|
|||
|
RE: [Python] Powerball number generator
@codecaine
wow thanks. i will have to study the random a bit more. 'shufftle' is very helpful here. ![]() i'm still thinking like java programmer.
![]()
|
|||
|
03-21-2010, 09:05 PM
Post: #6
|
|||
|
|||
|
RE: [Python] Powerball number generator
Yeah, mixing languages in the beginning is hard.
One experienced programmer said : "Don't learn new language until you finish the one that your started". Also you need to adjust your brain to think on a certain language ! There's a fine line between genius and insanity. I have erased this line. Oscar Levant There's a fine line between an administrator and black hat hacker. I have erased this line. Dr DEBCOL |
|||
|
03-22-2010, 11:00 AM
(This post was last modified: 03-22-2010 11:03 AM by codecaine.)
Post: #7
|
|||
|
|||
|
RE: [Python] Powerball number generator
Anytime my friend
There is a book I am readying over here. Its python "gray hat python programming". When I get some time I will make some examples. I should you how to use ctypes module in python and load dll files or if you on unix based systems .so files. Oh yea there is also a sort function built in python. mylist.sort() for it to change to list or if you wanted it to return a sorted list without changing the list itself do sorted(mylist)
|
|||
|
03-22-2010, 07:57 PM
Post: #8
|
|||
|
|||
|
RE: [Python] Powerball number generator
@codecaine
I was planning to take some time and study some sort algorithms. That is why I didn't use Python's built in sort. And if some beginner runs across this post, he could see an example of bubble sort. (Not that I'm a pro or something.... ). My teecha said that in this semester of my 'Introductions to computers' we will continue Python and that we will learn more about Databases, Networks and Object orientated programming (GUI) in Python. I was so excited and I stared jumping like a kid who got a candy...
![]()
|
|||
|
03-23-2010, 04:21 AM
Post: #9
|
|||
|
|||
RE: [Python] Powerball number generator
(03-22-2010 11:00 AM)codecaine Wrote: Anytime my friendThat built in function for sorting, is it quick sort ??? There's a fine line between genius and insanity. I have erased this line. Oscar Levant There's a fine line between an administrator and black hat hacker. I have erased this line. Dr DEBCOL |
|||
|
03-25-2010, 05:49 AM
Post: #10
|
|||
|
|||
|
RE: [Python] Powerball number generator
They have different type of sorting algorithms heres a link to show you http://wiki.python.org/moin/HowTo/Sorting
|
|||
|
« Next Oldest | Next Newest »
|




![[Image: 45669_pythonlogo.png]](http://myph.us/pics/45669_pythonlogo.png)



. For example,