0

everybody! I am currently learning Python (no prior coding skills - at least not enough to mention them) and I am struggling with a for loop in a lottery machine we are supposed to do for homework. I have spent the past two hours googling this, but I wasn't able to find anything that would hint me in the right direction.

I want the loop to create a list with random numbers. The amount of random numbers printed to the user should be defined by user input and it should not use duplicates. Every time a duplicate is created it should simply pick a different number. Any hints on where i screwed up here? (Pls note it's Python 2.x)

Thank you all! :)

Code:

from random import randint

lotterylist = []

print "Welcome to the lottery machine!"

mynumbers =  int(raw_input("How many numbers should be drawn?\n"))

for i in range(0, mynumbers):

    lottery_numbers = randint(0, 48)

    if lottery_numbers not in lotterylist:
        lotterylist.append(lottery_numbers)

    else:
        mynumbers += 1
        continue

print lotterylist

print "End"
  • 1
    Maybe use while instead of if else. – majidarif Nov 12 '18 at 12:59
  • 1
    Use [`random.sample()`](https://docs.python.org/3.5/library/random.html#random.sample) instead, without loop, e.g. `lottery_numbers = random.sample(range(48), mynumbers)`. – Vasilis G. Nov 12 '18 at 13:00
  • You should also post the input and generated output. Anyway, what you're doing is running your loop 'x' number of times, adding a new random number and doing nothing if it's the same. The flow is hence a bit flawed. – Nitin Pawar Nov 12 '18 at 13:02
  • As mentioned by @majidarif, a while implementation will make a simpler logic. – Nitin Pawar Nov 12 '18 at 13:03
  • 1
    @vasilis-g provided the perfect solution, but I'll suggest grinding with the loop, for learning. – Nitin Pawar Nov 12 '18 at 13:05
  • 1
    @NitinPawar indeed, using loop and conditions would be much more beneficial for the OP to learn. – Vasilis G. Nov 12 '18 at 13:06
  • `lottery_numbers = range(0, 48); random.shuffle(lottery_numbers); lottery_numbers[:mynumbers]` – Brown Bear Nov 12 '18 at 13:06
  • @NitinPawar will do, but at the moment functionality is more important then user comfort. But just for understanding: is it impossible to increase the range with a "+="? Also I am not sure how "while" differs from "if" in that case. Pls explain! We are supposed to solve the task with randint. Thank you all for your quick and useful help! – lichtgarage Nov 12 '18 at 13:42
  • You won't be able to change the number of iterations in this manner. What can be done is providing a simple comparison (in "while" preferably) between "mynumbers" (fixed) and a counter ("i", or "count", or anything, which will be incremented in "if" section and not in "else" section). – Nitin Pawar Nov 12 '18 at 14:57

1 Answers1

0

You could do it like this because honestly the current code doesn’t make much sense.

for _ in range(0, num):
  snum = randint(0, 48)
  while snum in numlist:
    snum = randint(0, 48)
  numlist.append(snum)

This will keep on creating a new number until a number not in the list is selected and then it exits the while loop and appends the number to your list of generated numbers. No need to keep on incrementing the range; going forward you should understand that if your logic requires something like that then it means it is flawed. Your current approach doesn’t make sense.

majidarif
  • 18,694
  • 16
  • 88
  • 133
  • Thank you for the solution! Works and I understand how! One more thing tho: if - for any reason - I wanted to increase the range of a loop similar to how I intended to do - is there any way? (Like: if this and that loop 5 instead of 4 times) – lichtgarage Nov 12 '18 at 14:23
  • @lichtgarage yes, and your question has been tagged as duplicate. The answer can be found there. – majidarif Nov 12 '18 at 14:24
  • Ah, nice feature! Thanks! :) – lichtgarage Nov 12 '18 at 14:37