[Python] Is there a way to fix this UnboundLocalError: local variable referenced before...

Discussão em 'Python' iniciado por Stack, Abril 8, 2025.

  1. Stack

    Stack Membro Participativo

    I have attempted to add a points system to this F1 game which updates after each race to eventually be ranked. However, even after referencing each driver's points at the start (second block), the function 'def race_result()' still says that they are undefined, although I am expecting each driver's points variable to change after each race. This is the error on its own:

    redbull_performance=1800
    Verstappen_pts=0 # defined here
    def race_result():
    Verstappen=95+redbull_performance+random.randint(1,1000)
    drivers=[Verstappen]
    pts=[0,25,18,15,12,10,8,6,4,2,1,0,0,0,0,0,0,0,0,0,0,0]
    drivers.sort(reverse=True)
    position=0
    print("Race")
    print("")
    print(" # Driver Team Country Pts")
    for i in range(20):
    if drivers[position]==Verstappen:
    r1="Max Verstappen Oracle Red Bull Racing - NED -"
    Verstappen_pts+=pts[position # can't find Verstappen_pts variable
    position+=1
    if position<10:
    print("",position,r1,pts[position])
    else:
    print(position,r1,pts[position])
    race_result()

    **Traceback (most recent call last):
    File "program.py", line 130, in <module>
    race_result()
    File "program.py", line 65, in race_result
    Verstappen_pts+=pts[position]
    UnboundLocalError: local variable 'Verstappen_pts' referenced before assignment**


    This is the full code - it worked perfectly before the points system:

    import random, time
    redbull_performance=1800
    mclaren_performance=1900
    ferrari_performance=1700
    williams_performance=1400
    mercedes_performance=1500
    astonmartin_performance=1300
    alpine_performance=1100
    vcarb_performance=1400
    sauber_performance=1000
    haas_performance=1300

    Verstappen_pts=0
    Norris_pts=0
    Leclerc_pts=0
    Piastri_pts=0
    Sainz_pts=0
    Russell_pts=0
    Hamilton_pts=0
    Alonso_pts=0
    Gasly_pts=0
    Hulkenberg_pts=0
    Tsunoda_pts=0
    Stroll_pts=0
    Ocon_pts=0
    Albon_pts=0
    Bearman_pts=0
    Lawson_pts=0
    Doohan_pts=0
    Antonelli_pts=0
    Hadjar_pts=0
    Bortoleto_pts=0

    def race_result():
    Verstappen=95+redbull_performance+random.randint(1,1000)
    Norris=94+mclaren_performance+random.randint(1,1000)
    Leclerc=93+ferrari_performance+random.randint(1,1000)
    Piastri=91+mclaren_performance+random.randint(1,1000)
    Sainz=89+williams_performance+random.randint(1,1000)
    Russell=87+mercedes_performance+random.randint(1,1000)
    Hamilton=89+ferrari_performance+random.randint(1,1000)
    Alonso=84+astonmartin_performance+random.randint(1,1000)
    Gasly=80+alpine_performance+random.randint(1,1000)
    Hulkenberg=79+sauber_performance+random.randint(1,1000)
    Tsunoda=81+redbull_performance+random.randint(1,1000)
    Stroll=78+astonmartin_performance+random.randint(1,1000)
    Ocon=81+haas_performance+random.randint(1,1000)
    Albon=82+williams_performance+random.randint(1,1000)
    Bearman=76+haas_performance+random.randint(1,1000)
    Lawson=82+vcarb_performance+random.randint(1,1000)
    Doohan=75+alpine_performance+random.randint(1,1000)
    Antonelli=79+mercedes_performance+random.randint(1,1000)
    Hadjar=77+vcarb_performance+random.randint(1,1000)
    Bortoleto=78+sauber_performance+random.randint(1,1000)
    drivers=[Verstappen,Norris,Leclerc,Piastri,Sainz,Russell,Hamilton,Alonso,Gasly,Hulkenberg,Tsunoda,Stroll,Ocon,Albon,Bearman,Lawson,Doohan,Antonelli,Hadjar,Bortoleto]
    pts=[0,25,18,15,12,10,8,6,4,2,1,0,0,0,0,0,0,0,0,0,0,0]
    drivers.sort(reverse=True)
    position=0
    print("Race")
    print("")
    print(" # Driver Team Country Pts")
    for i in range(20):
    if drivers[position]==Verstappen:
    r1="Max Verstappen Oracle Red Bull Racing - NED -"
    Verstappen_pts+=pts[position]
    elif drivers[position]==Norris:
    r1="Lando Norris McLaren - GBR -"
    Norris_pts+=pts[position]
    elif drivers[position]==Leclerc:
    r1="Charles Leclerc Scuderia Ferrari HP - MON -"
    Leclerc_pts+=pts[position]
    elif drivers[position]==Piastri:
    r1="Oscar Piastri McLaren - AUS -"
    Piastri_pts+=pts[position]
    elif drivers[position]==Sainz:
    r1="Carlos Sainz Williams Racing - ESP -"
    Sainz_pts+=pts[position]
    elif drivers[position]==Russell:
    r1="George Russell Mercedes AMG Petronas - GBR -"
    Russell_pts+=pts[position]
    elif drivers[position]==Hamilton:
    r1="Lewis Hamilton Scuderia Ferrari HP - GBR -"
    Hamilton_pts+=pts[position]
    elif drivers[position]==Alonso:
    r1="Fernando Alonso Aston Martin Aramco - ESP -"
    Alonso_pts+=pts[position]
    elif drivers[position]==Gasly:
    r1="Pierre Gasly Alpine - FRA -"
    Gasly_pts+=pts[position]
    elif drivers[position]==Hulkenberg:
    r1="Nico Hulkenberg KICK Sauber - GER -"
    Hulkenberg_pts+=pts[position]
    elif drivers[position]==Tsunoda:
    r1="Yuki Tsunoda Oracle Red Bull Racing - JPN -"
    Tsunoda_pts+=pts[position]
    elif drivers[position]==Stroll:
    r1="Lance Stroll Aston Martin Aramco - CAN -"
    Stroll_pts+=pts[position]
    elif drivers[position]==Ocon:
    r1="Esteban Ocon Haas - FRA -"
    Ocon_pts+=pts[position]
    elif drivers[position]==Albon:
    r1="Alex Albon Williams Racing - THA -"
    Albon_pts+=pts[position]
    elif drivers[position]==Bearman:
    r1="Oliver Bearman Haas - GBR -"
    Bearman_pts+=pts[position]
    elif drivers[position]==Lawson:
    r1="Liam Lawson Visa Cash App Racing Bulls - NZL -"
    Lawson_pts+=pts[position]
    elif drivers[position]==Doohan:
    r1="Jack Doohan Alpine - AUS -"
    Doohan_pts+=pts[position]
    elif drivers[position]==Antonelli:
    r1="Kimi Antonelli Mercedes AMG Petronas - ITA -"
    Antonelli_pts+=pts[position]
    elif drivers[position]==Hadjar:
    r1="Isack Hadjar Visa Cash App Racing Bulls - FRA -"
    Hadjar_pts+=pts[position]
    elif drivers[position]==Bortoleto:
    r1="Gabriel Bortoleto KICK Sauber - BRA -"
    Bortoleto_pts+=pts[position]
    position+=1
    if position<10:
    print("",position,r1,pts[position])
    else:
    print(position,r1,pts[position])
    race_result()

    Continue reading...

Compartilhe esta Página