Есть ли способ исправить этот несущественный диалект: локальная переменная, на которую ссылаются перед назначением?Python

Программы на Python
Ответить Пред. темаСлед. тема
Anonymous
 Есть ли способ исправить этот несущественный диалект: локальная переменная, на которую ссылаются перед назначением?

Сообщение Anonymous »

Я попытался добавить систему баллов в эту игру F1, которая обновляется после каждой гонки, чтобы в конечном итоге была ранжирована. Однако даже после ссылки на точки каждого водителя в начале (второй блок) функция «def race_result ()» все еще говорит, что они не определены, хотя я ожидаю, что переменная точек водителя будет изменена после каждой гонки. Это ошибка самостоятельно: < /p>
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
Это полный код - он отлично работал перед системой баллов: < /p>
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

Подробнее здесь: https://stackoverflow.com/questions/795 ... d-before-a
Реклама
Ответить Пред. темаСлед. тема

Быстрый ответ

Изменение регистра текста: 
Смайлики
:) :( :oops: :roll: :wink: :muza: :clever: :sorry: :angel: :read: *x)
Ещё смайлики…
   
К этому ответу прикреплено по крайней мере одно вложение.

Если вы не хотите добавлять вложения, оставьте поля пустыми.

Максимально разрешённый размер вложения: 15 МБ.

  • Похожие темы
    Ответы
    Просмотры
    Последнее сообщение

Вернуться в «Python»