And here's all the code, I posted screenshots for full context Code1 This is where I assume I'm missing something Code3
Code in text (forgive me for not doing this before, I'm quite new here, this is python 3 by the way)
#!/usr/bin/env python3
#David Martinez, December 5, 2022, CPT168-W47, Final Exam Project
#Description
import csv
import sys
FILENAME = "party.csv"
#numb=int(input("Enter no of guests/members:"))
#for i in range(numb):
# user = input("Enter your name:")
# utype = input("mem or guest:")
# food = input("Enter your menu choice beef or veg or chicken:")
#fees=21.0
#total=fees*numb
#g=0
#m=0
#for j in range(numb):
# if utype == 'guest':
# g=g 1
# else:
# m=m 1
#print('name',user)
#print('member or guest:',utype)
#print('food type:',food)
#print("fees paid:",fees)
#print("total members:",m)
#print("total guests:",g)
#print("total fees paid by all attendees",total)
#SPACE
def exit_program():
print("Terminating program.")
sys.exit()
def read_party():
try:
party = []
with open(FILENAME, newline="") as file:
reader = csv.reader(file)
for row in reader:
party.append(row)
return party
except FileNotFoundError as e:
return party
except Exception as e:
print(type(e), e)
exit_program()
def write_party(party):
try:
with open(FILENAME, "w", newline="") as file:
writer = csv.writer(file)
writer.writerows(party)
except OSError as e:
print(type(e), e)
exit_program()
except Exception as e:
print(type(e), e)
exit_program()
def list_party(party):
for i, party in enumerate(party, start=1):
print(f"{i}. {party[0]} --{party[1]}-- ({party[2]}) {party[3]}")
print()
def add_party(party):
#name = get_name
#member_guest = get_member_guest
name = input("Name: ")
member_guest = input("Member or Guest: ")
food = input(" Chicken entre Beef entre Vegetarian meal: ")
while True:
try:
fees = int(input("Fees: "))
except ValueError:
print("Invalid integer. Please try again.")
continue
if fees <= 0:
print("Fees must be greater than zero. Please try again.")
continue
else:
break
party = [name, member_guest, food, fees]
party.append(party)
write_party(party)
print(f"{name} was added.\n")
def delete_party(party):
while True:
try:
number = int(input("Number: "))
except ValueError:
print("Invalid integer. Please try again.")
continue
if number < 1 or number > len(party):
print("There is no member with that number. Please try again.")
else:
break
party = party.pop(number - 1)
write_party(party)
print(f"{party[0]} was deleted.\n")
def display_menu():
print("The Party List Program")
print()
print("COMMAND MENU")
print("list - List all members and guests")
print("add - Add a member to the list")
print("del - Remove a member from the list")
print("exit - Exit program")
print()
def main():
# party_list = [["Father Orion", member, 22.00, Chicken entre ],
# ["Mother Theresa", member, 22.00, ],
# ["Johnny", guest, 7.95, Vegetarian meal ]]
display_menu()
party = read_party()
while True:
command = input("Command: ")
if command.lower() == "list":
list_party(party)
elif command.lower() == "add":
add_party(party)
elif command.lower() == "del":
delete_party(party)
elif command.lower() == "exit":
break
else:
print("Not a valid command. Please try again.\n")
print("Bye!")
if __name__ == "__main__":
main()
I tried to make the user input a number that is then saved in the file in the last row, but for some reason everything gets spread out and the "fees" is rejected by the file in the error, again I'm not really experienced with this so I'd appreciate explanations too to see where I went wrong.
CodePudding user response:
party = [name, member_guest, food, fees]
party.append(party)
I'm going to guess that it's because you're appending party
to itself, thus creating a recursive list, and the csv writer can't handle that.