Good questions.
Quote:
Originally Posted by fusion1275
1) Why do the numbers print out surrounded in brackets i.e [2, 10, 35, 35, 47, 49] ??
|
The issue is this line
Code:
total = num1, num2, num3, num4, num5, num6 # Put all numbers in a variable
makes total into a tuple. It's the same as writing:
Code:
total = (num1, num2, num3, num4, num5, num6)
which is why it prints with brackets.
You could print them all out on the same line by doing
Code:
for i in total:
print i,
print
or
Code:
output=''
for i in total: output += str(i) + " "
print output
Quote:
3) Am I missing a way to shorten the code or write the code correctly? I never know if I am doing it right or wrong. Is it a case of "if it works, then its right?".
|
I'll answer this one next because the answer to this feeds into the next one. It is often difficult to work out whether you are doing it right or wrong - I guess it's something you learn with practice. I would say there's no "right" way to do it, but there are
definitely wrong ways to do it (and it's not as simple as "if it works, it's right" - perhaps "if it works, is concise, is easy to understand and maintain, is unlikely to be breakable by bad input, and is easily extensible, then you're on the right track")
The problem with your approach is this: suppose I asked you to generate 5000 numbers instead of just 6. With your approach, I suppose you would copy and paste the line 4994 times, then go through and change all the numbers on the end of 'num',
then go back and change all the ones you missed, and you'd probably still be missing loads. It's an ugly way to program, and it's horribly timeconsuming and error-prone.
A better way to do it is to use a
list (sometimes sort-of erroneously called an 'array' by python people).
Code:
my_list = [] # create a new list
while len(my_list) < 6: # while it has fewer than 6 elements...
my_list.append(random.randrange(49)+1) # ...add a new one
winners = sorted(my_list)
print winners
You can see that my code is shorter, easier to understand, and if I wanted 5000 numbers, I could just change "6" to "5000" and I'd be done.
You could just as easily have used a "for" loop, but I feel a "while" loop is more intuitive in this case.
Quote:
2) How do I prevent the numbers being duplicated "per run" as above "35, 35,"
|
There are a couple of ways you could do this, but one way would be to check each time if you're going to add a number that has been picked already:
Code:
my_list = [] # create a new list
while len(my_list) < 6: # while it has fewer than 6 elements...
new_number = random.randrange(49)+1 # store the new number in a variable
if not new_number in my_list: # actually almost plain english
my_list.append(new_number) # put it in the list!
winners = sorted(my_list)
print winners
Now there are still problems with this approach (what would happen if I changed the 6 to a 40? And what would happen if I changed the 6 to a 50?) but if you're only picking 6 numbers, it's probably good enough
Hope this helps,