-1

I'm trying to make a random menu generator by asking the user to input a food and every time they input it I would like it to start a new line in the .txt file but I get a 'string not supported error'

I have already tried taking away the str() before the 'file,"\n"' and I can't really find much to help me on other sites

name = open("food.txt", "a")
file = input("please enter food you like: ")
file = str(file,"\n")
name.write(file)

name.close()
open1 = open("food.txt", "r")
print (open1.read()) 

I expect it just to create a variable with a new line on the end of it but I get a 'decoding str is not supported' error

  • 1
    The second argument to `str()` is an encoding type, like `utf-8`. What do you mean by putting `"\n"` there? You don't need to call `str()`, since `input()` returns a string. – Barmar Nov 07 '19 at 18:52
  • 3
    I think what you mean is `file = file + "\n"` – Barmar Nov 07 '19 at 18:52
  • Possible duplicate of [Which is the preferred way to concatenate a string in Python?](https://stackoverflow.com/q/12169839/1092820) – Ruzihm Nov 07 '19 at 18:52
  • 1
    `input()` returns a string, at least it does in Python 3, so which version are you using? (Hint: Add the according tag). As a new user, also take the [tour] and read [ask]. – Ulrich Eckhardt Nov 07 '19 at 18:53
  • 2
    @UlrichEckhardt Based on the expected input, `input` would almost certainly raise a `NameError` if this were Python 2. – chepner Nov 07 '19 at 18:57

1 Answers1

1

The problem is a moot point if you just use print:

with open("food.txt", "a") as f:
    food = input("Please enter a food you like: ")
    print(food, file=f)
chepner
  • 497,756
  • 71
  • 530
  • 681