Reputation: 95
Basically, my program needs to be able to store recipes. I have to ask the user to input an ingredient along with the quantity and unit. I'm sorry if this is somewhat vague, but how can I program it so that I can ask the user to input multiple ingredients until they've entered the number of ingredients that they need to enter? Thank you very much.
#Ask the user to input ingredient name, quantity and measurement
ingredient = input("Name is your ingredient: ")
ingredientquantity = int(input("What is the quantity of this ingredient: "))
ingredientunit = input("What is the unit of your ingredient: ")
^^^ This is what I've written so far, but how could this be repeated and STILL STORE any previous entries?
Upvotes: 0
Views: 10536
Reputation: 113948
def validated_input(prompt,input_type=str):
while True:
try:
return input_type(raw_input(prompt))
except:
print "Error Invalid Input, Try Again"
def get_ingredient():
return validated_input("Ingredient Name:",str),validated_input("Qty:",int),validated_input("Units:",str)
ingredients = [get_ingredient() for _ in range(validated_input("Enter Number Of Ingredients:",int))]
Upvotes: 1
Reputation: 12077
Write a loop, and store them in a list. A while loop might be prudent here
ingredients = []
while True:
name = raw_input("Name is your ingredient: ")
quantity = int(raw_input("What is the quantity of this ingredient: "))
unit = raw_input("What is the unit of your ingredient: ")
ingredients.append((name, quantity, unit))
cont = raw_input("Continue adding ingredients? [y/n]")
if not cont.lower() in ("y", "yes"):
break
The code will ask for the stuff you wanted, and append these as a three tuple to a list after each iteration. Now once you're done (by answering anything but a y or yes), you'll have a list of three tuples (ingredient name, ingredient quantity, ingredient unit)
.
Have a look at the data structures part in the official documentation.
Demo:
Name is your ingredient: Flour
What is the quantity of this ingredient: 7
What is the unit of your ingredient: dl
Continue adding ingredients? [y/n]y
Name is your ingredient: Butter
What is the quantity of this ingredient: 50
What is the unit of your ingredient: gr
Continue adding ingredients? [y/n]y
Name is your ingredient: Sugar
What is the quantity of this ingredient: 1
What is the unit of your ingredient: dl
Continue adding ingredients? [y/n]n
>>> print ingredients
[('Flour', 7, 'dl'), ('Butter', 50, 'gr'), ('Sugar', 1, 'dl')]
EDIT: Changed the input
calls to raw_input
because in python 2.7, input
attempts to convert the input to whatever it thinks the input is. Thus numbers become integers and so forth. Not necessarily a good idea.
Upvotes: 4
Reputation: 3232
You can try something like:
data = []
i=0
max_cycles = 10
while i < max_cycles:
ingredient = input("Name is your ingredient: ")
ingredientquantity = int(input("What is the quantity of this ingredient: "))
ingredientunit = input("What is the unit of your ingredient: ")
data.append((ingredient, ingredientquantity, ingredientunit))
print(data)
i += 1
It wil iterate 10 times (max_cycles value) then break, and store all data as tuples inside data list.
Upvotes: 0