Day 052 - Project 52 : Pizza Pricer with Save/Load no crashing on non-int

If you have any questions, comments or issues with this project please post them here!

Hello Ian, I am on day 52 and am experiencing a problem. The code works except the .txt file is not getting appended, so the file and the viewing table is blank. I checked my code and it is very similar to the solution. If you could explain why this is happening, that would be great!
Thank you

Hey, @roninMehendale welcome to the forums!

Can you please provide a link to your repl?

1 Like

https://replit.com/@RadhikaMehendal/R-Day-52?v=1

In this case, you might actually want to use "w" instead of "a". Each time you read all the orders into the pizza list so when you write to the file, you are adding a whole new list. Instead, you should just overwrite the file with the pizza list and then it should work.

Thank you so much MattDESTROYER and not-ethan, your advice really helped!

3 Likes

Here’s my solution! Seems to work :slight_smile:

import os, time

debugMode=False
orders=[]

def autoLoad():
  global orders
  f=open("pizzas.txt", "r")
  orders=eval(f.read())
  f.close()

def autoSave():
  f=open("pizzas.txt", "w")
  f.write(str(orders))
  f.close()

try:
  autoLoad()

except Exception as err:
  print("Error: unable to load.")
  print(err)
  if debugMode:
    print(traceback)

def prettyPrint():
  global orders
  for row in orders:
    print(f"Name: {row[0]}, {row[1]} {row[2].upper()} Pizza(s).\nOrder Total = {row[3]}")
    print()
  print()
  time.sleep(3)
  os.system("clear")
  
def add():
  time.sleep(1)
  os.system("clear")
  pCost=0
  pizzaQ=0
  pizzaSz=""
  totalCost=0
  try:
    name=input("Name on the order: \n> \033[36m")
    print("\033[0m")
    pizzaQ=int(input("How many pizzas?\n> \033[36m"))
    print("\033[0m")
  except:
    print("Must enter a whole number (ex: 1, 3, 45, etc)\nTry again.")
    time.sleep(2)
    return
    
  pizzaSz=input("What size? (type S, M, L)").strip().lower()
  if pizzaSz=="s":
    pCost=5.99
  elif pizzaSz=="m":
    pCost=9.99
  elif pizzaSz=="l":
    pCost=13.99
  else:
    print("Not an option. Start over.")
    time.sleep(2)
    return
  totalCost=float(round(((float(pizzaQ)) * pCost), 2))

  time.sleep(1)
  os.system("clear")

  print(f"Thank you, {name}.")
  print()
  print(f"{pizzaQ} pizza(s) sized {pizzaSz.upper()}\nat {pCost} per {pizzaSz.upper()}")
  print(f"comes out to ${totalCost}")
  time.sleep(1)

  row=[name, pizzaQ, pizzaSz, totalCost]
  orders.append(row)

  autoSave()
  time.sleep(2)
  os.system("clear")
  
def view():
  time.sleep(1)
  os.system("clear")
  print("Current Orders:")
  prettyPrint()

while True:
  menu=input("add order\nview current orders\ntype 'add' or 'view'\n> \033[36m")
  print("\033[0m")
  if menu=="add":
    add()

  elif menu=="view":
    view()```

Hi, I keep getting this error when trying to test run my code: repl process died unexpectedly: signal: killed

image

image

I mean, you put the resources screenshot there, that’s a likely reason why then…

Yes that’s why I included it but can a small piece of code cause this?

It could be some kind of overload or something? I don’t know since I haven’t done the 100 days of Python before, but it is most likely related to those high levels

1 Like

I think I can track it down to a while True: loop that may be ‘slightly’ dysfunctional.

I had visions of not being able to complete the 100 days unless you had to purchase a mega account or something!

I’m running the code below and then getting the following syntax warning even though the program runs: “:1: SyntaxWarning: list indices must be integers or slices, not tuple; perhaps you missed a comma?”

Can someone help me understand what’s up with this warning? Thanks in advance.

import time
pizzadict = []

try: 
  f = open("pizza.orders","r")
  pizzadict = eval(f.read())
  f.close()
except:
  print("PIZZA ORDER FORM")

def viewPizza():
  h1 = "Name"
  h3 = "Size"
  h4 = "Quantity"
  h5 = "Total"
  print(f"{h1:^10}{h3:^10}{h4:^10}{h5:^10}")
  for row in pizzadict:
    print(f"{row[0]:^10}{row[1]:^10}{row[2]:^10}{row[3]:^10}")
  print()
  time.sleep(2)
  
while True:
  name = input("Your name: ")
  quantpizza = int(input("# of Pizzas: "))  
  sizepizza = input("Size pizza: ")
  pizzapricelist = {"small":10, "medium":15, "large":20}
  ordercost = int(pizzapricelist[sizepizza]) * int(quantpizza)

  pizzadict.append([name, quantpizza, sizepizza, ordercost])

  viewPizza()

  f = open("pizza.orders","a+")
  f.write(str(pizzadict))
  f.close()

Hello,
you should store your info as a text file, and don’t use eval(). (Since you are just appending data instead of replacing data in your file, your program will fail to read the file.)

Store your data as line-separated lists instead, instead of python code.

3 Likes

I suggest you avoid using eval and instead properly manage files.

4 Likes

I could be wrong @NuclearPasta0 and @whileTRUEpass, but if they’re using eval, it’s likely because the day said to use it.

2 Likes

You might be right. Than it is just a file name issue plus Day going the easy route :wink:

2 Likes