0
code = input("Voer een streepjescode in:")
code = int(code)
lijst = [int(i) for i in str(code)]
N1 = lijst[0]
N2 = lijst[1]
N3 = lijst[2]
N4 = lijst[3]
N5 = lijst[4]
N6 = lijst[5]
N7 = lijst[6]
N8 = lijst[7]
N9 = lijst[8]
N10 = lijst[9]
N11 = lijst[10]
N12 = lijst[11]
N13 = lijst[12]
N14 = lijst[13]
N15 = lijst[14]
N16 = lijst[15] 
N17 = lijst[16]
N18 = lijst[17]
print(N18)

So this is the code I have at the moment. I looked on google for a way to make the N1 to N18 variables using a loop (or at least something that does not take so much space), but I could not find anything. Is there a way to make this code look overal nicer and make it more uncluttered? I want that for example N16 has as value the 16th digit in the list of 'code' all the way to 18. The code I am working on does not go further than 18.

I tried using the following code:



for i in range(0, 18):
    globals()['N%s' % i] = lijst[i]


But it just gave me this IndexError: IndexError: list index out of range

jsotola
  • 2,238
  • 1
  • 10
  • 22
Clyft
  • 11
  • 1
  • 3
    Just name your list `N` and access `N[1]` etc. There's no point in assigning the values to individual variables here. – deceze Nov 29 '22 at 07:55
  • 2
    Yeah, **just don't do this at all**. It is the completely wrong approach. Don't dynamically create variables, use a *container*, like a `list` or a `dict`. You already *have* a list. So *just use the list* – juanpa.arrivillaga Nov 29 '22 at 08:31

0 Answers0