-1

How can I make this code more efficient? I'm changing a few of the numbers each time but it's getting tedious because I have so many more to go. I've tried:

i=1
print('Enter Number of #',i,'Tickets Left: ')
TL+i=input()
P+i+Y = int(T+i)-int(TL+i)

But am getting a syntax error.

P1 = 1
P2 = 50
P3 = 1
P4 =1
P5 = 2
P6=2
P7=2
P8=2
P9=2
P10=2
P11=2
P12=2
P13= 3
P14=3
P15=3
P16=5
P17=5


T1=250
T2=250
T3 = 250
T4 = 250
T5 = 250
T6 = 250    
T7 = 250
T8 = 250
T9 = 250
T10 = 250

for i in range(1,10):
    print('Enter Number of #1 Tickets Left: ')
    TL1 = input()
    P1y = int(T1) - int(TL1)
    
    print('Enter Number of #2 Tickets Left: ')
    TL2 = input()
    P2y = int(T2) - int(TL2) 
    

etc.

JamieC113a
  • 11
  • 1
  • 7
  • 5
    If you're ever numbering variables, you probably want to use a list: `p = [1, 50, 1, ...]` and then just access them with their index (starting at `0` eg., `p[0]`) – Kraigolas Aug 17 '22 at 20:28
  • 1
    Syntax error due to `TL+i=input()`. What are you trying to achieve with this line? – Vladimir Fokow Aug 17 '22 at 20:29
  • I would strongly recommend that you go through some Python tutorial, you would certainly find a lot of inspiration for better solutions. A list of some recommended ones: https://sopython.com/wiki/What_tutorial_should_I_read%3F – Thierry Lathuille Aug 17 '22 at 20:31
  • @VladimirFokow I suspect they're trying to build a variable name. Of course Python doesn't work like that. – Mark Ransom Aug 17 '22 at 20:32

1 Answers1

2

You can't modify a variable name dynamically the same way you can modify a string -- luckily you don't need to! Instead of having 10 different variables, create a single list with 10 values:

t_tickets = [250] * 10
p_tickets = [
    t - int(input(f"Enter Number of #{i} Tickets Left: "))
    for i, t in enumerate(t_tickets, 1)
]

t_tickets holds the ten 250 values (note that you don't really need a list for this purpose, but I assume that at some point these values might be different). We then iterate over that list to generate ten values for p_tickets by subtracting the user input from the corresponding t_tickets value.

Samwise
  • 68,105
  • 3
  • 30
  • 44