Question

In: Computer Science

having trouble with loop, when I added saving and loading to my code, when adding new...

having trouble with loop, when I added saving and loading to my code, when adding new player, it continues to ask for new player

class TeamClass:
    name = ""
    jersey = ""
    number = ""

    def __init__(self, name, jersey, number):
        self.name = name
        self.number = number
        self.jersey = jersey

    def set_name(self, name):
        self.name = name

    def set_jersey(self, jersey):
        self.jersey = jersey

    def set_number(self, number):
        self.number = number

    def get_name(self, name):
        return self.name

    def get_jersey(self, jersey):
        return self.jersey

    def get_number(self, number):
        return self.number

    def display_team_roster(self):
        print("Player")
        print("Name: ", self.name)
        print("Jersey: ", self.jersey)
        print("Phone Number: ", self.number)
        print("-------------")


def menu():
    print("-----Roster Menu----")
    print("1. Display Team Roster")
    print('2. New Player')
    print("3. Remove player")
    print("4. Edit Player")
    print("5. Save Roster Menu")
    print("6. Load Roster Menu")
    print("7. Exit Roster Menu")
    print("")
    selection = int(input("Enter an option or 7 to exit: "))
    return selection


def display_roster(roster):
    if len(roster) > 0:
        for x in roster.keys():
            roster[x].display_team_roster()
    else:
        print("Roster is empty")


def add_member(roster):
    new_member = input("Enter new Player: ")
    new_jersey = input("Enter jersey#: ")
    new_number = input("Enter phone number: ")
    roster[new_member] = TeamClass(new_member, new_jersey, new_number)


def del_member(roster):
    del_member = input("Which player would you like to remove?: ")
    if del_member in roster:
        del roster[del_member]
        print(del_member, "has been removed")
    else:
        print(del_member, "was not found")
    return roster


def edit_member(roster):
    existing_member = input("Which player would you like to edit? ")
    if existing_member in roster:
        new_member = input("Enter new players name: ")
        new_jersey = input("Enter new jersey#: ")
        new_number = input("Enter new phone number: ")
        roster[existing_member] = TeamClass(new_member, new_jersey, new_number)
        print(existing_member, "has been changed to", new_jersey, new_number)
    else:
        print(existing_member, "was not found")
    return roster


def save_member(roster):
    filename = input("week1.py\teamroster.txt:")
    print("Saving Roster")
    outfile = open(teamroster.txt,"wt")
    for x in roster.keys():
        name = roster[x].get_name()
        jersey = roster[x].get_jersey()
        number = roster[x].get_number()
        outfile.write(name+", "+jersey+", "+number+"\n")
    print("Roster saved")
    outfile.close()


def load_member():
    roster = {}
    filename = input("week1.py\teamroster.txt:")
    inFile = open(teamroster.txt,"rt")
    print("Loading Roster ")
    while True:
        inLine = inFile.readline()
        if not inLine:
            break
        inLine = inLine[:-1]
        name, jersey, number = inLine.split(",")
        roster[name] = TeamClass(name,jersey,number)
    print("Data Loaded Successfully.")
    inFile.close()
    return roster


print("")
roster = {}
menu_selection = menu()

while menu_selection != 7:
    if menu_selection == 1:
        display_roster(roster)
    elif menu_selection == 2:
        add_member(roster)
    elif menu_selection == 3:
        roster = del_member(roster)
    elif menu_selection == 4:
        roster = edit_member(roster)
    elif menu_selection == 5:
        roster = save_member(roster)
    elif menu_selection == 6:
        roster = load_member(roster)
menu_selection = menu()
print("Good Bye")

  

Solutions

Expert Solution

I have corrected all the errors and output is also given below and changes are highlighted

#problem is with while loop not terminating since the below statement
#menu_selection = menu() is written outside of while loop

class TeamClass:
    name = ""
    jersey = ""
    number = ""
    def __init__(self, name, jersey, number):
        self.name = name
        self.number = number
        self.jersey = jersey
    def set_name(self, name):
        self.name = name
    def set_jersey(self, jersey):
        self.jersey = jersey
    def set_number(self, number):
        self.number = number
    def get_name(self): #get_name() should accept only one argument i.e.,self but one more name argument is given similarly for get_ersey() and get_number() methods
        return self.name
    def get_jersey(self):
        return self.jersey
    def get_number(self):
        return self.number
    def display_team_roster(self):
        print("Player")
        print("Name: ", self.name)
        print("Jersey: ", self.jersey)
        print("Phone Number: ", self.number)
        print("-------------")
def menu():
    print("-----Roster Menu----")
    print("1. Display Team Roster")
    print('2. New Player')
    print("3. Remove player")
    print("4. Edit Player")
    print("5. Save Roster Menu")
    print("6. Load Roster Menu")
    print("7. Exit Roster Menu")
    print("")
    selection = int(input("Enter an option or 7 to exit: "))
    return selection
def display_roster(roster):
    if len(roster) > 0:
        for x in roster.keys():
            roster[x].display_team_roster()
    else:
        print("Roster is empty")
def add_member(roster):
    new_member = input("Enter new Player: ")
    new_jersey = input("Enter jersey#: ")
    new_number = input("Enter phone number: ")
    roster[new_member] = TeamClass(new_member, new_jersey, new_number)
def del_member(roster):
    del_member = input("Which player would you like to remove?: ")
    if del_member in roster:
        del roster[del_member]
        print(del_member, "has been removed")
    else:
        print(del_member, "was not found")
    return roster
def edit_member(roster):
    existing_member = input("Which player would you like to edit? ")
    if existing_member in roster:
        new_member = input("Enter new players name: ")
        new_jersey = input("Enter new jersey#: ")
        new_number = input("Enter new phone number: ")
        roster[existing_member] = TeamClass(new_member, new_jersey, new_number)
        print(existing_member, "has been changed to", new_jersey, new_number)
    else:
        print(existing_member, "was not found")
    return roster
def save_member(roster):
    filename = input("week1.py\teamroster.txt:")
    print("Saving Roster")
    outfile = open(filename,"wt") #filename should be written instead of directl giving teamroster.txt
    for x in roster.keys():
        name = roster[x].get_name()
        jersey = roster[x].get_jersey()
        number = roster[x].get_number()
        outfile.write(name+", "+jersey+", "+number+"\n")
        print("Roster saved")
    outfile.close()
def load_member():
    roster = {}
    filename = input("week1.py\teamroster.txt:")
    inFile = open(filename,"rt")#filename should be written instead of directl giving teamroster.txt
    print("Loading Roster ")
    while True:
        inLine = inFile.readline()
        if not inLine:
            break
        inLine = inLine[:-1]
        name, jersey, number = inLine.split(",")
        roster[name] = TeamClass(name,jersey,number)
        print("Data Loaded Successfully.")
    inFile.close()
    return roster
print("")
roster = {}
menu_selection = menu()
while menu_selection != 7:
    if menu_selection == 1:
        display_roster(roster)
    elif menu_selection == 2:
        add_member(roster)
    elif menu_selection == 3:
        roster = del_member(roster)
    elif menu_selection == 4:
        roster = edit_member(roster)
    elif menu_selection == 5:
        roster = save_member(roster)
    elif menu_selection == 6:
        roster = load_member() #load_member() is written without any arguments inside definition so i removed roster from load_member() function
    menu_selection = menu()
print("Good Bye")

  

Output


Related Solutions

I am having trouble with my assignment and getting compile errors on the following code. The...
I am having trouble with my assignment and getting compile errors on the following code. The instructions are in the initial comments. /* Chapter 5, Exercise 2 -Write a class "Plumbers" that handles emergency plumbing calls. -The company handles natural floods and burst pipes. -If the customer selects a flood, the program must prompt the user to determine the amount of damage for pricing. -Flood charging is based on the numbers of damaged rooms. 1 room costs $300.00, 2 rooms...
I'm having trouble with validating this html code. Whenever I used my validator, it says I...
I'm having trouble with validating this html code. Whenever I used my validator, it says I have a stray end tag: (tbody) from line 122 to 123. It's the last few lines. Thanks and Ill thumbs up whoever can help solve my problem. Here's my code: <!DOCTYPE html> <html lang="en"> <head> <title>L7 Temperatures Fields</title> <!--    Name:    BlackBoard Username:    Filename: (items left blank for bb reasons    Class Section: (blank)    Purpose: Making a table to demonstrate my...
I'm having trouble with my do while loop. I'm trying to get it where if the...
I'm having trouble with my do while loop. I'm trying to get it where if the user enter's 3 after whatever amount of caffeinated beverages they've entered before then the loop will close and the rest of my code would proceed to execute and calculate the average price of all the caffeinated beverages entered. Can someone please help me with this? Here's my Code: import java.util.Scanner; public class Main { public static void main(String[] args) { CaffeinatedBeverage[] inventory = new...
I am having trouble with a C++ code that I'm working on. It is a spell...
I am having trouble with a C++ code that I'm working on. It is a spell checker program. It needs to compare two arrays, a dictionary, and an array with misspelled strings that are compared to the strings in the dictionary. the strings that are in the second array that is not in the Dictionary are assumed to be misspelled. All of the strings in the dictionary are lowercase without any extra characters so the strings that are passed into...
I know how to do this with arrays, but I have trouble moving my code to...
I know how to do this with arrays, but I have trouble moving my code to use with linked lists Write a C program that will deal with reservations for a single night in a hotel with 3 rooms, numbered 1 to 3. It must use an infinite loop to read commands from the keyboard and quit the program (return) when a quit command is entered. Use a switch statement to choose the code to execute for a valid command....
I'm having trouble with my ZeroDenominatorException. How do I implement it to where if the denominator...
I'm having trouble with my ZeroDenominatorException. How do I implement it to where if the denominator is 0 it throws the ZeroDenominatorException and the catch catches to guarantee that the denominator is never 0. /** * The main class is the driver for my Rational project. */ public class Main { /** * Main method is the entry point to my code. * @param args The command line arguments. */ public static void main(String[] args) { int numerator, denominator =...
I am currently having trouble understanding/finding the errors in this python code. I was told that...
I am currently having trouble understanding/finding the errors in this python code. I was told that there are 5 errors to fix. Code: #!/usr/bin/env python3 choice = "y" while choice == "y": # get monthly investment monthly_investment = float(input(f"Enter monthly investment (0-1000):\t")) if not(monthly_investment > 0 and monthly_investment <= 100): print(f"Entry must be greater than 0 and less than or equal to 1000. " "Please start over.")) #Error 1 extra ")" continue # get yearly interest rate yearly_interest_rate = float(input(f"Enter...
Having trouble with the verilog code. Also think my ASM chart may be wrong so some...
Having trouble with the verilog code. Also think my ASM chart may be wrong so some help with that would be great as well thanks. Not sure if the question is linked but its a textbook question. Digital design 6th edition chapter 8 question 10.Link:  https://www.chegg.com/homework-help/Digital-Design-6th-edition-chapter-8-problem-10P-solution-9780134529561
Can you balance these redox equations? I am having trouble figuring out the process of adding...
Can you balance these redox equations? I am having trouble figuring out the process of adding extra products to make them work. ClO3- + Cl- -----> Cl2 + ClO2- Cr2O72- + C2O42- -----> Cr3+ + CO2
I am having trouble fixing my build errors. The compiler I am using is Visual Studio.Thanks....
I am having trouble fixing my build errors. The compiler I am using is Visual Studio.Thanks. #include <iostream> #include <string> #include <cstdlib> using namespace std; /* * structure to store employee details * employee details are stored as linked list */ struct Employee { private:    string full_name; //full name    double net_income; //income public:    struct Employee *next; //pointing to the next employee details in the list                        /*                   ...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT