gpt4 book ai didi

Python:将多行用户输入到文本文件

转载 作者:太空宇宙 更新时间:2023-11-04 00:12:30 25 4
gpt4 key购买 nike

我到底该怎么做...我是 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/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com