Getting python to export a menu and its choices to a file?

This is the place for queries that don't fit in any of the other categories.

Getting python to export a menu and its choices to a file?

Postby bryefellows » Sun Dec 08, 2013 8:24 pm

Hello guys,

I have a whole coffee shop menu that i want to write the choices to an external file but i dont know how. any help would be appreciated
I get an error when i try to run it and i want to get that whole menu on there. is there a way to put a whole menu on there and export to one file or does it have to be all different ones?

Brye


Code: Select all
countsal = 0
cobbsal = 1.50

def writesalads(salname, countsal):
   saladlist = open("saladsfile.txt", 'w')
   saladscost = open("saladsfilecost.txt", 'w')
   saladslist.write(salname)
   saladscost.write(countsal)
   
      
      
def main():   
   global saladslist
   global saladscost
   saladslist = open("saladsfile.txt", 'w')
   saladscost = open("saladsfilecost.txt", 'w')
   whatever = "y"
   while whatever[0] == "y":
      print "1. Cobb"
      print "2. Caeaser"
      choicesal = raw_input("which salad")
      if choicesal in ["cobb", "1", "cob"]:
         countsal = input("How many cobb salads would you like?")
         writesalads(choicesal, countsal)
         whatever = raw_input("yes no")
   saladslist.close()
   saladscost.close()      

main()

This is what i have for the menu
Code: Select all

# -*- coding: cp1252 -*-
#Brian Fellows Coffee shop calculator
#main

def main():       
#Define costs for each item
#Salad price   
   ceasersalad = 6.50
   cobbsalad = 6.00
   chefsalad = 5.50
   spinachdijonsalad = 5.25
   bbqsalad = 5.00
#Drink Price   
   coffee = 1.75
   mtndew = 1.00
   pepsi = 1.00
   orangejuice = 1.75
   soda = 1.25
#Sandwich Price   
   bltsandwich = 6.00
   fatcatsandwich = 6.50
   roastbeefsandwich = 4.50
   turkeysandwich = 5.50
   heartattacksandwich = 5.00 
#Dessert Price
   pudding = 2.75
   fruit = 2.50
   cake = 2.00
   pie = 1.75
   jello = 1.25
#Total variables   
   total = 0
   totalsandwich = 0
   totalsalad = 0
   totaldrinks = 0
#set count for each item to 0
#Salad count   
   ceasersaladcount = 0
   cobbsaladcount = 0
   chefsaladcount = 0
   spinachdijonsaladcount = 0
   bbqsaladcount = 0
#Drink Count   
   coffeecount = 0
   mtndewcount = 0
   pepsicount = 0
   orangejuicecount = 0
   sodacount = 0
#Sandwich Count
   bltsandwichcount = 0
   fatcatsandwichcount = 0
   heartattacksandwichcount = 0
   turkeysandwichcount = 0
   roastbeefsandwichcount = 0
#Dessert Count
   puddingcount = 0
   fruitcount = 0
   cakecount = 0
   piecount = 0
   jellocount = 0
#greet user         
   customer_order = "yes"
   print "Welcome to the Mocha Café"
   print "Please make your selection by number or word"
   print ""
   print ""
   print ""
#print the menu to the user         
   while customer_order[0] == "y":
      print "Please select from the following menu:"
      print "Category: Salads"
      print "1. Ceasar $6.50"
      print "2. Cobb $6.00"
      print "3. Chef $5.50"
      print "4. Spinach Dijon Salad $5.25"
      print "5. BBQ Salad $5.00"
      print ""
      print "Category: Drinks"
      print "6. Coffee $1.75"
      print "7. Orange Juice $1.50"
      print "8. Soda $1.25"
      print "9. Mountain Dew $1.00"
      print "10. Pepsi $1.00"
      print ""
      print "Category: Sandwiches"
      print "11. Fat Cat Sandwich $6.50"
      print "12. BLT $6.00"
      print "13. Turkey $5.50"
      print "14. Heart Attack $5.00"
      print "15. Roast Beef Sandwich $4.50"
      print ""
      print "Category: Desserts"
      print "16. Pudding 2.75"
      print "17. Fruit $2.50"
      print "18. Cake $2.00"
      print "19. Pie $1.75"
      print "20. Jello 1.25"
      print "--------------------------------------------------"
      item=raw_input("Please enter the item you want to purchase (by number or word):")


# if else nest for items on menu
      if item in ["1", "ceaser"]:
         ceasersaladnum = int(input("How many ceaser salads would you like?")) 
         ceasersaladcount = ceasersaladnum + ceasersaladcount 
         total = total + (ceasersalad * ceasersaladcount )
         customer_order = raw_input("Would you like to order more items(y/n):")
         
#elif parts of statement to check for more items and add on                
#Salads
      elif item in ["2", "cobb"]:
         cobbsaladnum = int(input("How many cobb salads would you like?")) 
         cobbsaladcount = cobbsaladnum + cobbsaladcount
         total = total + (cobbsalad * cobbsaladcount)
         customer_order = raw_input("Would you like to order more items(y/n):")
         
      elif item in ["3", "chef"]:
         chefsaladnum = int(input("How many chef salads would you like?")) 
         chefsaladcount = chefsaladnum + coffeecount 
         total = total + (chefsalad * chefsaladcount)
         customer_order = raw_input("Would you like to order more items(y/n):")
         
      elif item in ["4", "spinach dijon"]:
         spinachdijonnum = int(input("How many spinach dijon salads would you like?")) 
         spinachdijonsaladcount = spinachdijonnum + spinachdijonsaladcount 
         total = total + (spinachdijonsalad * spinachdijonsaladcount)
         customer_order = raw_input("Would you like to order more items(y/n):")
      
      elif item in ["5", "bbq salad"]:
         bbqsaladnum = int(input("How many BBQ salads would you like?")) 
         bbqsaladcount = bbqsaladnum + bbqsaladcount 
         total = total + (bbqsalad * bbqsaladcount)
         customer_order = raw_input("Would you like to order more items(y/n):")
#Drinks
      elif item in ["6", "coffee"]:
         coffeenum = int(input("How many coffees would you like?")) 
         coffeecount = coffeenum + coffeecount 
         total = total + (coffee * coffeenum)
         customer_order = raw_input("Would you like to order more items(y/n):")
                  
      elif item in ["7", "orange juice"]:
         orangejuicenum = int(input("How many orange juices would you like?")) 
         orangejuicecount = orangejuicenum + orangejuicecount 
         total = total + (orangejuice * orangejuicenum)
         customer_order = raw_input("Would you like to order more items(y/n):")
         
      elif item in ["8", "soda"]:
         sodanum = int(input("How many sodas would you like?")) 
         sodacount = sodanum + sodacount 
         total = total + (soda * sodanum)
         customer_order = raw_input("Would you like to order more items(y/n):")
         
      elif item in ["9", "mountain dew"]:
         mtndewnum = int(input("How many mountain dews would you like?")) 
         mtndewcount = mtndewnum + mtndewcount 
         total = total + (mtndew * mtndewnum)
         customer_order = raw_input("Would you like to order more items(y/n):")
         
      elif item in ["10", "pepsi"]:
         pepsinum = int(input("How many pepsi's would you like?")) 
         pepsicount = pepsinum + pepsicount 
         total = total + (pepsi * pepsinum)
         customer_order = raw_input("Would you like to order more items(y/n):")
#Sandwiches
      elif item in ["11", "fat cat sandwich"]:
         fatcatsandwichnum = int(input("How many fat cats would you like?")) 
         fatcatsandwichcount = fatcatsandwichnum + fatcatsandwichcount 
         total = total + (fatcatsandwich * fatcatsandwichnum)
         customer_order = raw_input("Would you like to order more items(y/n):")
         
      elif item in ["12", "BLT"]:
         bltsandwichnum = int(input("How many blt's would you like?")) 
         bltsandwichcount = bltsandwichnum + bltsandwichcount 
         total = total + (bltsandwich * bltsandwichnum)
         customer_order = raw_input("Would you like to order more items(y/n):")
      
      elif item in ["13", "turkey"]:
         turkeysandwichnum = int(input("How many turkey sandwiches would you like?")) 
         turkeysandwichcount = turkeysandwichnum + turkeysandwichcount 
         total = total + (turkeysandwich * turkeysandwichnum)
         customer_order = raw_input("Would you like to order more items(y/n):")
      
      elif item in ["14", "heart attack"]:
         heartattacknum = int(input("How many heart attack sandwiches would you like?")) 
         heartattacksandwichcount = heartattacknum + heartattacksandwichcount 
         total = total + (heartattacksandwich * heartattacknum)
         customer_order = raw_input("Would you like to order more items(y/n):")
      
      elif item in ["15", "roast beef"]:
         roastbeefnum = int(input("How many roast beef sandwiches would you like?")) 
         roastbeefsandwichcount = roastbeefnum + roastbeefsandwichcount 
         total = total + (roastbeefsandwich * roastbeefnum)
         customer_order = raw_input("Would you like to order more items(y/n):")
         
      elif item in ["16", "pudding"]:
         puddingnum = int(input("How many puddings would you like?")) 
         puddingcount = puddingnum + puddingcount 
         total = total + (pudding * puddingnum)
         customer_order = raw_input("Would you like to order more items(y/n):")
      
      elif item in ["17", "fruit"]:
         fruitnum = int(input("How many fruit would you like?")) 
         fruitcount = fruitnum + fruitcount 
         total = total + (fruit * fruitnum)
         customer_order = raw_input("Would you like to order more items(y/n):")
      
      elif item in ["18", "cake"]:
         cakenum = int(input("How many cakes would you like?")) 
         cakecount = cakenum + cakecount 
         total = total + (cake * cakenum)
         customer_order = raw_input("Would you like to order more items(y/n):")
         
      elif item in ["19", "pie"]:
         pienum = int(input("How many pies would you like?")) 
         piecount = pienum + piecount 
         total = total + (pie * pienum)
         customer_order = raw_input("Would you like to order more items(y/n):")
         
      elif item in ["20", "jello"]:
         jellonum = int(input("How many jellos would you like?")) 
         jellocount = jellonum + jellocount 
         total = total + (jello * jellonum)
         customer_order = raw_input("Would you like to order more items(y/n):")   
#this part tells the user they have entered invalid items         
      else:
         print "You have entered an invalid item."
         main()
#Receipt printing out part of program   
   print ""      
   print "Here is your receipt!"
   print ""
   print "You ordered:"
   print ""
   print "Category: Salads"
   print "%s Ceaser Salads" % (ceasersaladcount)
   print "%s Cobb Salads" % (cobbsaladcount)
   print "%s Chef Salads" % (chefsaladcount)
   print "%s Spinach Dijon Salads" % (spinachdijonsaladcount)
   print "%s BBQ Salads" % (bbqsaladcount)
   print "You ordered %s salads for a subtotal of %.2f" % ((ceasersaladcount + cobbsaladcount
         + chefsaladcount + spinachdijonsaladcount + bbqsaladcount), (ceasersaladcount * ceasersalad) +
         (cobbsaladcount * cobbsalad) + (chefsaladcount * chefsalad) + (spinachdijonsaladcount * spinachdijonsalad) + (bbqsaladcount * bbqsalad))
   print ""
   print "Category: Drinks"
   print "%s Coffees" % (coffeecount)
   print "%s orange juices" % (orangejuicecount)
   print "%s Sodas" % (sodacount)
   print "%s Mountain Dews" % (mtndewcount)
   print "You ordered %s drinks for a Subtotal of %.2f" % ((coffeecount + orangejuicecount +
      sodacount + mtndewcount + pepsicount), (coffeecount * coffee) + (orangejuicecount * orangejuice) +
      (sodacount * soda) + (mtndewcount * mtndew) + (pepsicount * pepsi))
   print ""
   print "Category: Sandwiches"
   print "%s Fat Cat Sandwiches" % (fatcatsandwichcount)
   print "%s BLT's" % (bltsandwichcount)
   print "%s Turkey Sandwiches" % (turkeysandwichcount)
   print "%s Heart Attack Sandwiches" % (heartattacksandwichcount)
   print "%s Roast Beef Sandwiches" % (roastbeefsandwichcount)
   print "You ordered %s sandwiches for a subtotal of %.2f" % ((fatcatsandwichcount + bltsandwichcount +
      turkeysandwichcount + heartattacksandwichcount + roastbeefsandwichcount),
      (fatcatsandwichcount * fatcatsandwich) + (bltsandwichcount * bltsandwich)
    + (turkeysandwichcount * turkeysandwich) + (heartattacksandwichcount *
       heartattacksandwich) + (roastbeefsandwichcount * roastbeefsandwich))
   print ""
   print "Category: Desserts"
   print "%s Pudding(s)" % (puddingcount)
   print "%s Fruit(s)" % (fruitcount)
   print "%s Cake(s)" % (cakecount)
   print "%s Pie(s)" % (piecount)
   print "%s Jello(s)" % (jellocount)
   print "You ordered %s desserts for a subtotal of %.2f" % ((puddingcount + fruitcount +
      cakecount + piecount + jellocount), (puddingcount * pudding) + (fruitcount * fruit)
    + (cakecount * cake) + (piecount * pie) + (jellocount * jello))
#this part prints the total
   print ""
   print "Total"
   print ""   
   print "You purchased %s items for a total of %.2f" % (ceasersaladcount + cobbsaladcount
         + chefsaladcount + spinachdijonsaladcount + bbqsaladcount + coffeecount +
         orangejuicecount + sodacount + mtndewcount + pepsicount + fatcatsandwichcount
         + bltsandwichcount + turkeysandwichcount + heartattacksandwichcount +
         roastbeefsandwichcount + puddingcount + fruitcount +
      cakecount + piecount + jellocount, total)
   print ""
   print "Sales tax %.2f" % (total *.07)
   print ""
   print "Your total with sales tax is %.2f" % (total * 1.07)
main ()      
bryefellows
 
Posts: 6
Joined: Mon Nov 04, 2013 11:05 pm

Re: Getting python to export a menu and its choices to a fil

Postby hansn » Sun Dec 08, 2013 10:26 pm

If you want to write all of the user input to a file, you would have to open the file with 'a' (append) mode in stead of 'w' (write) mode, as write mode will overwrite the file every time you open it.

Also, post your entire traceback when you're asking for help with an error. I think it's caused by you trying to write an integer to a file on line 8, and you should convert it to a string before writing it.
hansn
 
Posts: 87
Joined: Thu Feb 21, 2013 8:46 pm

Re: Getting python to export a menu and its choices to a fil

Postby bryefellows » Sun Dec 08, 2013 11:17 pm

hansn wrote:If you want to write all of the user input to a file, you would have to open the file with 'a' (append) mode in stead of 'w' (write) mode, as write mode will overwrite the file every time you open it.

Also, post your entire traceback when you're asking for help with an error. I think it's caused by you trying to write an integer to a file on line 8, and you should convert it to a string before writing it.


here's the traceback

Traceback (most recent call last):
File "/Users/brianfellows/Downloads/external files.py", line 33, in <module>
main()
File "/Users/brianfellows/Downloads/external files.py", line 28, in main
writesalads(choicesal, countsal)
File "/Users/brianfellows/Downloads/external files.py", line 11, in writesalads
saladscost.write(countsal)
TypeError: expected a character buffer object
>>>
bryefellows
 
Posts: 6
Joined: Mon Nov 04, 2013 11:05 pm

Re: Getting python to export a menu and its choices to a fil

Postby snippsat » Mon Dec 09, 2013 1:56 am

A quick example.
Code: Select all
countsal = 1
>>> saladscost.write(countsal)
Traceback (most recent call last):
  File "<interactive input>", line 1, in <module>
TypeError: expected a character buffer object

input() in python 2 return an integer,and write() need a string.
Here 1 get written to "saladsfilecost.txt" with no problem because it's a string.
Code: Select all
>>> countsal = '1'
>>> saladscost = open("saladsfilecost.txt", 'w')
>>> saladscost.write(countsal)

The code has some stuff that could be better like the use of global(don't use it).
A to long "elif" chain(look into dictionary,list for other ways to solve it).
User avatar
snippsat
 
Posts: 221
Joined: Thu Feb 21, 2013 12:04 am

Re: Getting python to export a menu and its choices to a fil

Postby bryefellows » Tue Dec 10, 2013 12:20 am

Ok,

So in that program i got it to generate a file called menuorder.txt. How would I get it to take all of those choices in the if and elif statements and append or write to that file?
bryefellows
 
Posts: 6
Joined: Mon Nov 04, 2013 11:05 pm

Re: Getting python to export a menu and its choices to a fil

Postby snippsat » Tue Dec 10, 2013 5:00 am

How would I get it to take all of those choices in the if and elif statements and append or write to that file?

A example with dictionary,to give you an idea of how to shorten all your repeating code.
If you modify a little you can make one calculation function,that do same calculation as in that long "elif" chain.
Code: Select all
def food(arg):
    return {
    "1" : ("ceaser", 6.50),
    "2" : ("cobb", 6.00),
    "3" : ("chef", 5.50),
    }.get(arg, 'Key not in food')

def showoptions():
    print "1. Ceasar $6.50"
    print "2. Cobb $6.00"
    print "3. Chef $5.50"

def menu():
    while True:
        showoptions()
        opt = raw_input("Enter food number: ")
        if opt in ['1', '2', '3']:
            print 'Your choice was {} at price {}'.format(food(opt)[0], food(opt)[1])
        else:
            print "{} is not a valid option,try again.".format(opt)

menu()
User avatar
snippsat
 
Posts: 221
Joined: Thu Feb 21, 2013 12:04 am

Re: Getting python to export a menu and its choices to a fil

Postby ochichinyezaboombwa » Tue Dec 10, 2013 6:34 am

bryefellows wrote:Ok,
How would I get it to take all of those choices in the if and elif statements and append or write to that file?

-- Why?
ochichinyezaboombwa
 
Posts: 200
Joined: Tue Jun 04, 2013 7:53 pm


Return to General Coding Help

Who is online

Users browsing this forum: W3C [Linkcheck] and 4 guests

cron