It is currently 19 Feb 2018, 23:28

Anything GoesCreating your own Lottery Quick Pick Generator in CodeSkulptor (Showcase | Slideshow)

Anything, almost anything at all
(even advertisement if it's decent, spammers will be banned)
Post Reply
User avatar

View: Showcase | Slideshow
Topic Posts: 1/1
Posts: 2160
Joined: 26 Sep 2016, 00:16
Cash on hand: 7,933.94 GLP
Bank: 51,356.25 GLP
Gender: Guy
Location: Victoria, BC
Operating System: Windows
GIMP Version: 2.8.22
My Personal Blog
My FineArtAmerica
x 1207
x 256

Creating your own Lottery Quick Pick Generator in CodeSkulptor

Post#1 by tim » 30 Sep 2016, 22:18

This tutorial assumes that you're not a programmer, it teaches you basic concepts to build your own Lottery Quick Pick Generator.
We'll be building a very simple program in Codeskulptor.
1. Hold Ctrl (on windows) and Click this link --> to open it in a new browser tab.
On the page, it shows up with some sample code in the left window. Delete all that code. We'll be adding our own code.
2. Insert the below code.

Code: Select all

import random
print random.randrange(0,3)
3. Click the Play button (the triangle button) to run program a few times and observe that a number between 0 and 2 is printed out (on right window) each time we run program.
The first line "import random" simply tells the program to import/include a library called "random" which gives us functions/methods that we can use to get random numbers.
In the second line, "print" just prints whatever we tell it to print, and "random.randrange(0,3)" uses the function/method randrange that belong to the "random" library, passing in parameters 0 and 3, which will return a random number from 0 to 3 (excluding 3).
So for example if we wanted to print out random number between 0 and 10, we can use "random.randrange(0,11)".
4. Now delete the code we have and use this code

Code: Select all

import random
numbers = []
print numbers
5. Run the program by click on the Play button. And you'll see a list of 3 numbers 1, 3, and 5.
Looking at the code, the first line we know about but we don't use it in this program because we're not calling random anything.
The second line defines an empty list called "numbers". In Codeskulptor, an empty list is "[]".
The third, fourth and fifth lines just keep adding a number to our list. We first add 1 to the list, so it's "[1]" then we add 3 to end of list so it's "[1,3]"
and then we add 5 to end of list so it's "[1,3,5]". Then the last line simply prints out result so we can see on the Right window.
6. Now delete the code we have and use this code

Code: Select all

import random
number = 0
while number < 10:
    print number
    number = number + 1
7. Click play and you'll see numbers printed from 0 to 9 (on right window). First line, we know about. Second line we set a variable called "number" to zero. Next line is a "while" loop. Any code that is indented (must have the same indentation for example if you tab then all the code part of the while loop must be tabbed the same, if you uses spaces for example 2 spaces, then all lines inside the while loop must be 2 spaces from the left margin) is part of the while loop. On the line "while number < 10:", we're saying do the following actions while number is less than 10. The ":" symbol is to say that next block of code must be indented the same way and is what the while loop does. So then it runs the next line, which prints out our number, then the next line "number = number + 1" means that when we run this line our number variable is set to its own value + 1 so it's incremented. Then it returns to the while loop line and checks if the condition is still true, if true it runs again...That's how we see 0 to 9 because each time through the loop the condition (number < 10) is true until it is no longer true then the code exits the loop and continues.
8. Now delete that code and use the below code.

Code: Select all

import random
numbers = [1,3,5]
if 2 in numbers:
    print "2 is in list"
    print "2 is not in list"
9. The first line we already know. The second line defines a variable called "numbers" and is set to contain a list of 3 numbers 1,3, and 5. The next line is an if statement. it's saying if 2 is in our list called "numbers". Again we see the ":" symbol which says run the next block of indented code (provided the condition (2 in numbers) is true). then un-indented "else:" which is part of the "if" statement but for the "else" case which means if (2 in numbers) is false then it'll run the "else" case.
10. Click play to run the code and you'll see that it prints out "2 is not in list" because 2 isn't in our list [1,3,5].
11. Delete the code and use the below code.

Code: Select all

import random
numbers = [6,2,4]
my_sorted_list = sorted(numbers)
print my_sorted_list
12. The first line we know about, the 2nd line we also know about. The 3rd line, we're setting a variable called "my_sorted_list" to the result of calling a method/function called "sorted" passing it list of numbers. The function sorts our list and returns the result. Then the next line simply prints out the sorted result held in "my_sorted_list" variable.
13. Run program by clicking play button, and you'll see the printed list (on right window) is a sorted list of "numbers" variable.
14. Delete the code and use the below code.

Code: Select all

import random
numbers = [1,2,3,4,5]
print len(numbers)
15. Run the code by clicking on Play button. You'll see the number 5 printed because that's the length (here we use the function/method called len to return length of list) of our variable "numbers" which holds a list of 5 items (numbers from 1 to 5).
16. Okay, now we know enough to write a simple Lottery Quick Pick Generator.
17. Delete the code and use code below

Code: Select all

import random
numbers = []
while len(numbers) < 6:
    new_number = random.randrange(0,49)+1
    if new_number not in numbers:
print sorted(numbers)
18. Read each line of code carefully and see if you still remember what each line does from examples shown above.
First line imports/includes random library.
Next line defines a variable called "numbers" which holds the empty list "[]"
Then the next line we do a while (length of numbers) is less than 6:
Next line (inside the while loop) we set a variable called "new_number" to hold a random number between 0 and 49 (excluding 49) with 1 added to it so the result is a number between 1 and 49 (inclusively).
Next line, we do an "if" check to see if "new_number" is not in our "numbers" list then the next line runs if the condition is true.
Next line, we append the new_number to our "numbers" list.
So basically this whole while loop keeps generating random numbers, and adds it to list if it's not already in our list and this loops stops when length of list is equal to 6 because (len(numbers < 6) is no longer true.
Then finally we print out our numbers list using the learned "sorted" function/method.
19. Click play a few times to see that each time we click play, it generates a different set of sorted numbers which can be used for lotteries where you choose 6 numbers out of 49 (like the Canadian Lottery 6/49).
20. Congratulations, now you're a programmer.
Extra notes: Codeskulptor is a Python language interpreter written in Javascript. If you'd like to learn more on doing/programming in CodeSkulptor you can learn about the Python programming language.
0 x

aka Tin

Registered members enjoy ads-free content
Post Reply

Last 20 Members Who Visited This Topic

  • Similar Topics
    Last post
• Featuring Members' Works (Image Attachments Only) •
2: Ella
3: Meemaw
4: gnerd
5: Pat625
6: tim
7: oldmangrumpy

Who is online

Users browsing this forum: No registered users and 1 guest