NameError: name 'Class' is not defined (Python) -
NameError: name 'Class' is not defined (Python) -
so i'm trying create save , load files text based game... saving works, , overwrites save if save again. loading brings nameerror.
def save(): my_file = open("save.txt", "w") my_file.write(class + "\n") my_file.write(level + "\n") my_file.write(str(hp) + "\n") my_file.write(str(atk) + "\n") my_file.write(str(def) + "\n") my_file.write(str(spd)+ "\n") my_file.write(str(ene)+ "\n") my_file.write(str(race1)+ "\n") my_file.close() def load(): infile = open("save.txt") lines = infile.readlines() line_number = 0 while line_number < len(lines): class = lines[line_number] level = lines[line_number + 1] hp = lines[line_number + 2] atk = lines[line_number + 3] def = lines[line_number + 4] spd = lines[line_number + 5] ene = lines[line_number + 6] race1 = lines[line_number + 7] line_number += 8 print(class, level, hp, atk, def, spd, ene, race1) infile.close() identify() above save , load definitions..
here class input definition:
def class_level(): global class global level class = input("please input class: ") print() level = input("please input level: ") print() race() and here indentify definition:
def identify(): global hp global atk global def global spd global ene if re.match(r"warrior", class, re.i): <---- line error on, class not defined print() can tell me i've done wrong? load file , display it.. class not defined in def identify. thanks.
in python way global works not define variable global variable. adds variable local namespace global namespace if present. if not present, creates global instance , adds local namepsace. might have class variable in global namespace, within identify function local namespace, class not present. need add together class local namespace of identify. adding line global class. local namespace has reference the variable class
def identify(): global hp global atk global def global spd global ene # need mention global class global class if re.match(r"warrior", class, re.i): print() python nameerror defined
Comments
Post a Comment