我正在尝试通过要求用户输入食物来制作随机菜单生成器,每次输入食物时,我都希望它在.txt文件中开始新行,但出现“字符串不支持的错误” '
我已经尝试过删除'file'\ n''之前的str()了,在其他站点上我找不到真正的帮助
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())
我希望它只是在变量的末尾添加一个新行,但出现“不支持解码str”错误
答案 0 :(得分:1)
如果您仅使用print
,那么问题就来了,
with open("food.txt", "a") as f:
food = input("Please enter a food you like: ")
print(food, file=f)