0

Im making a program that displays data from the Keno API (Keno is a gambling "site" where you need to pick numbers and match those numbers to the ones the game picks). This API gets this data and allows the user to input their picks and see if they won.

But now to my problem, In Keno you can pick a certain amount of numbers. For example you can pick 3 numbers, (this is referred to as spot 3, pick 5 numbers and thats spot 5 and so on.). And each of these spots have their own win list. Now my script has these spot winlists in tables that are named c_spotx_WinList where x is the spot.

In this simplfied code snippet I have built a smaller version of what I'm trying to achieve

number = 1
index = 4
Table1 = [0, 1, 2, 3, 4, 5]
Table2 = [0, 10, 20, 30, 40, 50]
while True:
    result = Table + number[index]
    print(result)

On line 6 is where I want to add the value of number to be added to the end of Table to create Table1 from which I can use Index to get the requested value.

Is this even possible? Also if you want to see the orginal code it is viewable here: Main.py and WinList.py

Catotron
  • 13
  • 5
  • 1
    Instead of a sequence of variable names, use a list or dictionary. – Klaus D. Jul 17 '23 at 01:06
  • 1
    Right. Don't use variable variable names. This is what dictionaries and lists are for. – Tim Roberts Jul 17 '23 at 01:11
  • Dictionary variant of @TimRoberts answer: `number = 1` `index = 4` `tables = {'Table1': [0, 1, 2, 3, 4, 5], 'Table2': [0, 10, 20, 30, 40, 50]}` `result = tables['Table' + str(number)][index]` `print(result)` - keeps same numbering as and looks more similar to your original code. I would not recommend it over the approach in the answer though: this is a situation where it makes more sense to use a `list` with numbered indices than a `dict` with named keys. – Quack E. Duck Jul 17 '23 at 01:35

1 Answers1

1

Here is the right way to do this kind of thing.

number = 1
index = 4
Tables = [
 [0, 1, 2, 3, 4, 5],
 [0, 10, 20, 30, 40, 50]
]

result = Tables[number][index]
print(result)

Output:

40
Tim Roberts
  • 48,973
  • 4
  • 21
  • 30
  • This seems to do the trick, I'm yet to add it to the main project yet. But a quick question does the first table in `Tables` get set to index 0? – Catotron Jul 17 '23 at 01:16
  • 1
    Yes, lists in Python are numbered from 0. If you really want indexes to start from one, then just use `number-1` when you index it. – Tim Roberts Jul 17 '23 at 01:29
  • Yep, got it to work. Thanks for your help – Catotron Jul 17 '23 at 01:31