Reputation: 23
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
Upvotes: 2
Views: 1028
Reputation: 107104
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');
Upvotes: 2
Reputation: 71610
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:
Upvotes: 0
Reputation: 18697
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']
Upvotes: 1