I have a couple of issues with my lab assignment for this week. I was wondering if you guys could help. I will post the assignment and then post my code thus far. Thank you.
)Write a program that will display the menu below until user decide the quit the program.
High Score Keepers
0 - Quit
1 - List Scores
2 - Add a Score
b) For the option Quit- if the user select this option they will be prompted to exit the program
c) List Scores - if user select this option the program should read from a text file and display in a list format the user name and score.
d) Add a score
If user enter this option the user should enter the player's name and score.
Next your program should sort the information by player's score. Your program should keep only the top 5 scores and write this to a text file.
e) If a player enters a choice other than 0,1 or 2 your program should display an error message to the user.
f) Exactly how you choose to format the file will be left up to you this time. However, you must use a delimiter of some kind to separate the name and score values in the text file.
-My issues are when I select option 2 it gives me an error from the
line = inFile.readline() , I don't understand this since that line is in a totally different if statement.
-My other issue is that I need to loop the menu, but if I put the user input variable (option) in the function I can't call it in my if statements.
-option 0 and 1 all work perfectly fine on their own.
#Lab 9: High Scores Keeper import sys mitems = ["0 - Quit", "1 - List Scores", "2 - Add a Score"] def menu(): print ("High Score Keeper") print (mitems) print (mitems) print (mitems) option = raw_input("Please choose an option (0-2): ") menu() if option == "0": quit = raw_input("Are you sure you would like to quit? (Y/N): ") if quit == "y" or quit == "Y": sys.exit(0) elif quit == "n" or quit == "N": menu() else: print ("Invalid Response: Reloading Main Menu") menu() if option == "1": inFile = open ("highscores.txt", "r") while True: line = inFile.readline() if line == "": break #reached the end of the data file line = line.rstrip ('\n') #remove trailing linefeed line = line.strip() #remove leading and trailing blanks if line.isdigit(): score = int(line) print score else: name = line print name inFile.close() menu() if option == "2": eName = raw_input("Enter player's name: ") eScore = raw_input("Enter player's score: ") add =  add.append(eName) add.append(eScore) print add
Edited 7 Years Ago by hockfan86: Trying to get code tags to work.