2

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

3 Answers 3

2

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');
Sign up to request clarification or add additional context in comments.

2 Comments

This goes straight to a one lined text file though
Good catch. Corrected then. Thanks.
1

Try with readlines() on sys.stdin:

>>> import sys
>>> sys.stdin.readlines()
first line 
second line
third line
<Ctrl+D>
['first line\n', 'second line\n', 'third line\n']

Comments

0

OR using a while loop, and keep appending to list:

l=[]
a=input()
while a:
   l.append(a)
   a=input()
print(l)

Example output:

First line
Second line

['First line', 'Second line']

Or for full code:

reciept_name=input('Reciept: ')
a=input('Your input: ')
with open(reciept_name+'.txt','w') as f:
   while a:
      f.write(a+'\n')
      a=input('Your input: ')

Example Output:

Reciept: Bob
Your input: 123
Your input: 456
Your input: abc
Your input: 

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.