python - Why is PyCharm giving me an Unresolved Reference NameError -


i'm taking tutorial learn python. i've written code see below:

creatures = []  def get_creature_race():     creature_race = []     creature in creatures:         creature_race.append(creature["race"].title())     return creature_race   def print_creature_race():     creature_race = get_creature_race()     print(creature_race)   def add_creature(race, sex):     creature = {"race": race, "creature_sex": sex}     creatures.append(creature)   def save_file(creature):     try:         f = open("creatures.txt", "a")         f.write(creature + "\n")         f.close()     except exception:         print("could not save file")   def read_file():     f = open("creatures.txt", "r")     creature in f.readlines():         add_creature(creature)     f.close()   read_file() print_creature_race()   creature_race = input("enter creature race: ") creature_sex = input("enter creature sex: ")  add_creature(creature_race, creature_sex) print_creature_race() save_file(creature_race) 

when try run following:

...line 32, in read_file     add_creature(creature) typeerror: add_creature() missing 1 required positional argument: 'sex' 

line 32 4th line in read_file() function definition:

add_creature(creature) 

if hover on right parenthesis ')' get:

parameter 'sex' unfilled 

if put second parameter in:

def read_file():     f = open("creatures.txt", "r")     creature in f.readlines():         add_creature(creature, sex)     f.close()     print("could not read file") 

i get:

line 32, in read_file     add_creature(creature, sex) nameerror: name 'sex' not defined 

when hover on sex gives me:

unresolved reference sex 

if put try block around def read_file() can run program without erroring out:

def read_file():     try:         f = open("creatures.txt", "r")         creature in f.readlines():             add_creature(creature, sex)         f.close()     except exception:         print("could not read file") 

and run following:

could not read file [] enter creature race: giant enter creature sex: male ['giant']  process finished exit code 0 

the first line exception read_file() function because creatures.txt doesn't exist yet (or thought). [] output print_creature_race() function since empty. next 2 "enter..." statements should self-explanatory. ['giant'] output second print_creature_race() function call. have creatures.txt file word 'giant' in it.

now, according tutorial, if run second time should is:

giant  enter creature race: elf enter creature sex: female 

there's no exception because file creatures.txt exists. print_creature_race() function call results in 'giant' being printed. , 2 prompts enter race , sex. however, is:

could not read file [] enter creature race:  

if continue prompts get:

could not read file [] enter creature race: elf enter creature sex: female ['elf'] 

my creatures.txt file has giant , elf (each on own line) but, obviously, still can't read file. definition add_creature has 2 parameters error both including , not including it.

what doing wrong?

please learn how debug code! i'll give hints. these, however, not lead succes (as paul cornelius pointed out)

fix last line:

save_file(creature_race)

to

save_file(creatures)

brief explanation: saving race, while functions await both: race , sex.

you should review hot appropriatelly save data in file , read it. (check python+csv example).

also: functions created bad - not encapsulated. try rethink code in way, none of functions have call or change external variables. can start code, without executing of functions, , call them 1 one see do. watch @ global variables changing , fix code on go.


Comments

Popular posts from this blog

ZeroMQ on Windows, with Qt Creator -

unity3d - Unity SceneManager.LoadScene quits application -

python - Error while using APScheduler: 'NoneType' object has no attribute 'now' -