0

Basically I am trying to make a game, where I generate 15 different numbers between 1 and 15, and then assign them all to one variable for each number.

I tried using random.sample but it wasn't able to assign the numbers to a single variable per number. Alternatively, I tried typing out 'if' statements, determining if there is duplicates, but it didn't work, and takes a long time.

numbers = random.sample(range(15), 15)
print(numbers)

I hoped that I could somehow assign each number to a variable, for example, the first number it generates will be called firstnumber, secondnumber will be the second number generated.

nickdbb12
  • 1
  • 1
  • Why you can't assign those 15 values to one list instead of creating 15 variables. It's also saving your memory. – Kushan Gunasekera May 24 '19 at 02:29
  • This is a [duplicate](https://stackoverflow.com/q/1373164/4518341), but why do you need this? A list seems to be the right datatype. – wjandrea May 24 '19 at 02:45
  • 2
    Possible duplicate of [How do I create a variable number of variables?](https://stackoverflow.com/questions/1373164/how-do-i-create-a-variable-number-of-variables) – Stedy May 24 '19 at 02:59

1 Answers1

0

You should use a list and append to it:

vars = []
for _ in range(x):
    vars.append(random.choice(y))

But if you really want your method, you can use a dictionary:

vars = {}
for i in range(x):
    vars[f'var{i+1}'] = random.choice(y)
Alec
  • 8,529
  • 8
  • 37
  • 63