Issue
There was a looping error with add, but now that I can add data to my txt file, there is this error using 'see' to list my data. Below is the erro:
TypeError:list_champs() takes 0 positional arguments but 1 was given.
I cannot see where I added a parameter unknowingly that doesn't need one in my current code.
# file = open("champs.txt", "w+")
FILENAME = "champs.txt"
def write_champs(champs):
with open(FILENAME, "w") as file:
for champ in champs:
file.write(champ + "\n")
def read_champs():
champs = []
with open(FILENAME) as file:
for line in file:
line = line.replace("\n", "")
champs.append(line)
return champs
def list_champs():
for i in range(len(champs)):
champ = champs[i]
print(str(i+1) + " - " + champs)
print()
def add_champ(champs):
champ = input("Champion: ")
#year = input("Season: ")
#champ = []
champs.append(champ)
#champ.append(year)
write_champs(champs)
print(champ + " was added.\n")
def display_menu():
print("Premier League Champions")
print("++++++++++++++++++++++++")
print("COMMANDS")
print("see - See the list of Champions")
print("add - Add a Champion to the list")
print("exit - Exit program")
print()
def main():
display_menu()
champs = read_champs()
while True:
command = input("Enter command: ")
if command == "see":
list_champs(champs)
elif command == "add":
add_champ(champs)
elif command == "exit":
print("Later!")
break
else:
print("Input not valid. Try again.")
if __name__ == "__main__":
main()
As always help is much appreciated!
Solution
You need to change your def list_champs
to support arguments:
def list_champs(champs):
for i in range(len(champs)):
champ = champs[i]
print(str(i+1) + " - " + champs)
print()
Answered By - Robinson Answer Checked By - Senaida (PHPFixing Volunteer)
0 Comments:
Post a Comment
Note: Only a member of this blog may post a comment.