I am writing a function that is reading an already processed file (file_name). In the processed file (open_file1) all lines are tuples. My problem is in this: in order to use the program, I have to always start with the command input as input file name. If the user enters TEAM IDENTIFIER i.e. the third elif statement without entering the input file name, then the program crashes. So, what I did was that in the third elif statement I checked the existence of the input file with an os statement. If the input file did not exist, I wrote an else statement to ask for another command (i.e. to input the input file name and start allover again). But, for some reason, when I am entering command as TEAM IDENTIFIER, the program is not working on the else statement of the third elif statement. Any suggestions? Thanks in advance.
def main():
command=input('Input cmd: ')
while command.lower() !='quit':
if command.lower()=='help':
print("QUIT\n"\
"HELP\n"\
"INPUT filename\n"\
"TEAM identifier\n"\
"REPORT n HITS\n"\
"REPORT n BATTING\n"\
"REPORT n SLUGGING\n")
elif command.startswith('INPUT') and len(command)>6:
file_name=str(command[6:])
open_file1=new_list(file_name)
print(open_file1)
elif command.startswith('TEAM') and len(command)>5:
if os.path.isfile(open_file1):
team_name=str(command[4:])
for line in open_file1:
print(line[1])
else:
command=input('Input cmd: ')
command=input('Input cmd: ')
main()
ERROR:
Traceback (most recent call last):
File "C:\Users\Dasinator\Documents\Books IX\Python Examples\textbook examples\project 07\openfile.py", line 98, in <module>
main()
File "C:\Users\Dasinator\Documents\Books IX\Python Examples\textbook examples\project 07\openfile.py", line 81, in main
if os.path.isfile(open_file1):
UnboundLocalError: local variable 'open_file1' referenced before assignment