Can I get some sort of jumpstart with a python script? I've been on this same project for over a year and can't for the life of me seem to get started on it..
Code:
The assignment: You've been hired to write a program that will choose random lottery numbers for the Lotto 6-49. Your program should create a list of 6 different random numbers in the range of 1 to 49, then print them all out on one line.
He says it has to do with "structured data" in a program. Any help would be appreciated.
import random
possibleNumbers = range(1, 50)
random.shuffle(possibleNumbers)
randomNumbers = [str(number) for number in possibleNumbers[0:6]]
print(', '.join(randomNumbers))
import random
possibleNumbers = range(1, 50)
random.shuffle(possibleNumbers)
randomNumbers = [str(number) for number in possibleNumbers[0:6]]
print(', '.join(randomNumbers))
possibleNumbers is, if I'm not mistaken, an array holding values from 1 to 50.
random.shuffle is a method from the random class (I can't speak with too much confidence here since I have little idea how Python is structured internally). Basically it takes an array and randomly swaps different values to different indices. e.g: [1,2,3,4,5] -> [3,2,5,4,1]
This process is done in-place, meaning it performs the operation on the array you give it and not produce anything new. It's like cooking a corn, if you will.
str(number) serves to type-cast the number into a string format, so it follows that randomNumbers is a list of strings. (e.g: randomNumbers = ["12","15","6","7",...])
The whole line translates into English as: Take the first 6 values in the possibleNumbers array (which was shuffled randomly), convert each of them into a string and store them in their respective index of the list randomNumbers.
Here's a more Python noob-friendly way of doing it.
Code:
import random
randomNumbers = []
while len(randomNumbers) < 6:
randomNumber = random.randint(1, 49)
if randomNumber not in randomNumbers:
randomNumbers.append(randomNumber)
for eachRandomNumber in randomNumbers:
print(str(eachRandomNumber) + " ")
Python is a bit strange with endpoints compared to other languages. You'll just have to get used to that. :-)
And keep in mind that if you are unsure about what values will be in a list or something, such as endpoints, just experiment by typing commands into the python interpreter. Interpreters are great for learning and understanding programming languages.
Experimenting is the spirit of learning this business anyway. It'd just be helpful if you can make reasonable assumptions here and there instead of having to rely on empirical experience.
Fiel Wrote:range(1, 50) creates a range from [1, 50)
Here's a more Python noob-friendly way of doing it.
Code:
import random
randomNumbers = []
while len(randomNumbers) < 6:
randomNumber = random.randint(1, 49)
if randomNumber not in randomNumbers:
randomNumbers.append(randomNumber)
for eachRandomNumber in randomNumbers:
print(str(eachRandomNumber) + " ")
Yeah, that'll probably make my teacher less hard, thus less suspicious. Looks like something I would come up with, thanks Fiel
I hate to double post, but I didn't want to start a new thread.
Here is the second part of this assignment:
Code:
Your previous lottery number generator would work for selecting Lotto 6/49 numbers but other lotteries use different sets of numbers and different lengths of lists. It would be much more useful if we could have a program that would let the user input the minimum and maximum values for the random generated numbers as well as the number of random numbers needed, like this:
>>> How many random numbers do you need generated?
5
>>> What is the minimum value of random number?
1
>>> What is the maximum value of random number?
20
>>> [2,17,4,12,9]
Write a program called lotto2.py that will get input from the user for the number of random numbers to be generated, the minimum value and the maximum value. It should include a function called "numlist" that will take those three values as parameters then return (*not print*) the list of random values.
Now my brainstorming got me to the point of using inputs in place of the min and max(1, 50) and the number of numbers(:6), I tired using variables for them, but I guess I'm too much of a noob to do any of this.. Also, I have no idea of where to use this "numlist" he speaks of.
Heidi Wrote:So does it mention about what functions, parameters, return values, etc are?
Considering the pages being "very long", I'd assume yes. Who could write pages on introduction to programming without those concepts?
Anyway, this is how you do it:
Instead of explicitly declaring the values like Fiel did
here
Code:
while len(randomNumbers) < 6:
and here
Code:
randomNumber = random.randint(1, 49)
, you implicitly refer to those numbers via variables: