0

I am trying to code my own food selector in python where people can pick if they want a tortilla in their wrap for example and the program will work out the costs.

One of the extra's I am trying to do is that, if the customer does not want to checkout immediately they can save their order to a text file. That all works perfectly but how do I rename this text file to the user_name so that only their choices are displayed and so I know who has made these choices?

    if order_save =="1":

        f = open ("user_name.txt","w")
    f.close()       
    if Tortilla_option == "yes":
        f = open ("user_name.txt","a")
        f.write("Tortilla")
        f.close()

    if Chicken_option == "yes":
        f = open ("user_name.txt","a")
        f.write("\nChicken")
        f.close()

    if Salad_option == "yes":
        f = open ("user_name.txt","a")
        f.write("\nSalad")
        f.close()

    if Chilli_option == "yes":
        f = open ("user_name.txt","a")
        f.write("\nChilli Sauce") 
        f.close()

print ("Here is your takeway that will be saved",user_name,"!\n")
f = open ("user_name.txt","r")
print(f.read())
f.close()
help-info.de
  • 6,695
  • 16
  • 39
  • 41
  • When I give the option for a service charge, the service charge and the cost of the takeaway get added up How come 2.50+2.31 is £4.8100000000000005? – Cameron Morris Nov 22 '16 at 19:46
  • Because many (most?) decimal numbers can't be represented exactly as floats. You shouldn't use floats for money. See http://stackoverflow.com/questions/588004/is-floating-point-math-broken for more details. – John Gordon Nov 22 '16 at 20:46

2 Answers2

1

Several things about this code. First:

f = open ("user_name.txt","a")
f.write("\nChilli Sauce") 
f.close()

can be written as

with open(user_name+'.txt', 'a') as f:
    f.write('\nChilli Saurce')

that manages the closing of the file automatically. It's called a context manager

Also, that saves the order into a file the name of which depends on who the user is. Notice that user_name is the variable, not a string.

However, a better strategy would be do define a function:

def write_order(user, item, option_yes_or_no):
    if option_yes_or_no=='yes':
        with open(user+'.txt', 'a') as f:
            f.write(item)

and then you replace the whole thing with:

for my_option in [tortilla_option, chicken_option, salad_option]:
    write_order(user_name, item, my_option)

which avoids duplicating a lot of code.

elelias
  • 4,552
  • 5
  • 30
  • 45
  • When I give the option for a service charge, the service charge and the cost of the takeaway get added up How come 2.50+2.31 is £4.8100000000000005? – Cameron Morris Nov 22 '16 at 20:07
  • Computers are not able to represent numbers with infinite precision, as that would require infinite memory. In this case, this means that the number that the computer can store that is *closer* to the actual number 4.81 is the one you are seeing. – elelias Nov 22 '16 at 21:02
0

Define the filename and open the file at the top of your code:

filename = '%s.txt' % user_name
f = open (filename, "w")

If you don't care about saving the filename for later use, you can even combine these into one line:

f = open ('%s.txt' % user_name, "w")

Then do all the file writes, and close the file when you're finished.

Opening, writing, and closing the file multiple times is wasteful and error-prone.

John Gordon
  • 29,573
  • 7
  • 33
  • 58
  • When I give the option for a service charge, the service charge and the cost of the takeaway get added up How come 2.50+2.31 is £4.8100000000000005? – Cameron Morris Nov 22 '16 at 20:08