Skip to content
Advertisement

Python: Making a multiple line user input to text file

How the heck can I do this… I’m New to Python and I’m trying to create a recipe catalog program to store all my recipes…

The ‘adding a recipe’ part of my code:

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 ());

I just need the Multi-line user input…

EDIT: I need it to go into a .txt file

Advertisement

Answer

You can use iter(input, '') to accept input until a blank line:

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');
User contributions licensed under: CC BY-SA
1 People found this is helpful
Advertisement