我到底怎么才能做到这一点...我是 Python 新手,我正在尝试创建一个菜谱目录程序来存储我所有的菜谱...
我的代码的“添加菜谱”部分:
def add_recipe():
recipe_name = input ("Recipe name: ");
print('Please add the directions AND ingredients')
with open (recipe_name + ".txt", "x") as f:
f.write(input ());
我只需要多行用户输入...
编辑:我需要它进入 .txt 文件
最佳答案
您可以使用 iter(input, '')
接受输入,直到出现空行:
recipe_name = input ("Recipe name: ");
print('Please add the directions AND ingredients')
with open (recipe_name + ".txt", "x") as f:
for line in iter(input, ''):
f.write(line + '\n');
关于Python:将多行用户输入写入文本文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52031215/