There are few issues in your code:
park
and chosen_park
are local variables inside new_park()
, so they cannot be accessed inside confirm_park()
. They need to be declared global inside new_park()
.
You should read the park list outside new_park()
and just randomly choose a park inside the function.
you are not saving the chosen_park
to stl_parks_visited.csv
.
park
is a list of park names with trailing "
" in each park name. Use park = f.read().splitlines()
to remove the trailing "
".
According to your code, it seems like that stl_park_list.csv
contains the park name only in each line. So using pandas
to convert the list park
to dataframe and save the dataframe to file is not necessary. Just use simple file operation to append chosen_park
to output file is enough.
Below are modified new_park()
and confirm_park()
:
with open("stl_parks_list.csv") as f:
park = f.read().splitlines()
def new_park():
global chosen_park
chosen_park = random.choice(park)
canvas.itemconfig(park_text, text=chosen_park)
def confirm_park():
park.remove(chosen_park)
print(len(park))
with open("data/stl_parks_visited.csv", "a") as f:
print(chosen_park, file=f)
With the above change, the GET PARK button is not necessary.
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…