-2
 f=open('foo.dat','w+')
 rate=0
 print"Menu is:"
 print"""1. Indian
           2.Italian
           3.Chinese
           4.COntinential
           5.Starters and drinks"""
 hotel_food={1:'Indian',2:'Italian',3:'Chinese'}
 f.write(str(hotel_food))
 food=input("Enter the food type:")
 f.write(str(food))
 if(hotel_food.has_key(food)):
      print"Menu is:"
      print"""1. Roti with Curry
           2.South Indian Cuisines
           3.festival dishes
           4.sea food"""
      hotel_food1={1:'Roti with Curry',
           2:'South Indian Cuisines'}
      f.write(str(hotel_food1))
      fd=input("ENter the food type")
      f.write(str(fd))
      if(hotel_food1.has_key(fd)):
            rate=rate+1234
            print rate

  l=f.write(str(rate))

  x=f.read(l)
  print x

 f.close()

This program allows user to input the type of food they want and the subtype as well. after that the bill or rate is calculated.

When I run the program, the error is: x=f.read(l)
TypeError: an integer is required

but when I make it into : x=f.read(str(l))
the same error comes: x=f.read(str(l))
TypeError: an integer is required.

Here, only one case is shown even though the menu has 5 cases, that is selecting Indian food and roti with curry. If this gets correct, I will do the rest.

Is this the right way to implement files in python? I can't write anything into file.

Kara
  • 6,115
  • 16
  • 50
  • 57
Aanandhi V B
  • 79
  • 2
  • 10
  • That's not how you use [`read`](https://docs.python.org/3/tutorial/inputoutput.html#methods-of-file-objects). The parameter to `read` lets you specify a certain amount of bytes to read from the file. – Morgan Thrapp Nov 10 '15 at 17:42
  • i want to print rate just to check whether its written into file.how do you check it? – Aanandhi V B Nov 10 '15 at 17:45

1 Answers1

1

Your issue is here: x=f.read(l)

When you pass a single argument to read() is expects and integer (how many bytes to read IIRC).

Use x=f.read() with no argument to read all data from the file.

Also l=f.write(str(rate)) is unnecessary. Just call f.write(str(rate)) no need to store return value, unless you're expecting something to go wrong.

EDIT: I have a few recommendations: Use a string rather than an int and a key for your dictionaries. This way raw_input() will provide a key you can use directly.

Also, you are reading a writing to the same file too often, it is messing up your reading. See here for more pythonic handling of files. I would recommend encapsulating your reads and write this way.

If you just want to check that you have written the data to a file, simply use f.write(data) and assume it work unless it throws an error. See my answer here.

I'm not entirely sure what is supposed to happen, but here is my best guess

f=open('foo.dat','w+')
rate=0
print"Menu is:"
print"""1. Indian
        2.Italian
        3.Chinese
        4.COntinential
        5.Starters and drinks"""
hotel_food={'1':'Indian','2':'Italian','3':'Chinese'}
f.write(str(hotel_food))
food=raw_input("Enter the food type: ")
f.write(str(food))
if(food in hotel_food):
    print"Menu is:"
    print"""1. Roti with Curry
    2.South Indian Cuisines
    3.festival dishes
    4.sea food"""
    hotel_food1={'1':'Roti with Curry', '2':'South Indian Cuisines'}
    f.write(str(hotel_food1))
    fd=raw_input("ENter the food type: ")
    f.write(str(fd))
    if fd in hotel_food1:
        rate=rate+1234
        f.write(str(rate))
        x=f.read()
        print x

f.close()

Hope this help!

Community
  • 1
  • 1
Will
  • 4,299
  • 5
  • 32
  • 50