### Thread: Extracting from a loop

1. No Profile Picture
Registered User
Devshed Newbie (0 - 499 posts)

Join Date
Mar 2014
Posts
6
Rep Power
0

#### Extracting from a loop

Hi

I am an ABSOLUTE novice at Python so if I am doing this completely wrong I apologise!

I am trying to produce a 'simple' calcualtor that allows users to enter data with the overall aim to converting it at the end. I have got so far (as in it works but may be an incorrect way of obtaining the orginal data) but am now stuck on how to extract the data at the end. I have searched but it is very hard when I'm not exactly sure what I'm looking for!

Code:
recipename=(input("What is your meal called? "))

ingQuant = int(input("How many ingredients are in this meal? "))

print ("The number of ingredients in",recipename,"is",ingQuant)
people=int(input("How many people will this serve? "))
print ("This recipe will serve",people,"people")
print ('\n')

for loopCounter in range(ingQuant):
print("Ingredient number",loopCounter+1)
ingmeasure=float(input("How much do you need? (grams): "))

print ('\n')

print ('\n')

print("All ingedients entered, Thank you")

print ('\n')
print("--------------")
print ('\n')
print("To calcualate recipe for",recipename,"for a new number of people")

newpeople=int(input("What is your new number of people to be served? "))

print ('\n')
print("--------------")
print ('\n')
So, I am now looking for there to be some code saying:

Code:
NewIngQuant = ingmeasure/people
NewIngMeasure = NewIngQuant * newpeople
The problem is, if the user has entered 5 ingredients, I need this to repeat 5 times, with the value associated earlier in the loopCounter.

I'd be looking for a final statement saying:

To make recipename for newpeople you will need:
NewIngMeasure of Water
NewIngMeasure of Potatoes
NewIngMeasure of Carrots
NewIngMeasure of Rice
NewIngMeasure of Noodles
As I said, I just don't know how to define the different ingredients and in turn the NewIngMeasure as different...

Lots of inaccuracies in the code/how I've explained this but any help would be useful! As I said, total beginner, I wont be able to follow complex answers!

I'm using Python 3.3

Thanks
2. You should be capturing the ingredients in a dictionary. So where you now have:

Code:
for loopCounter in range(ingQuant):
print("Ingredient number",loopCounter+1)
ingmeasure=float(input("How much do you need? (grams): "))
I recommend:

Code:
ingredients={}
for loopCounter in range(ingQuant):
print("Ingredient number",loopCounter+1)
ingmeasure=float(input("How much do you need? (grams): "))
ingredients[ingname]=ingmeasure
Then you can access a list of ingredients with:
Code:
ingredients.keys()
and retrieve the amount for each with for instance:
Code:
for ingnm in ingredients:
amount=ingredients[ingnm]
3. No Profile Picture
Registered User
Devshed Newbie (0 - 499 posts)

Join Date
Mar 2014
Posts
6
Rep Power
0
Thank you!

I think this is exactly what I am after.

After adding the first bit in the loop I wrote the rest at the bottom.

Code:
ingredients.keys()

for ingnm in ingredients:
amount=ingredients[ingnm]
I then printed ingredients to see what I had done and got:

{'Potato': 500.0, 'Water': 1000.0, 'Carrots': 300.0}
So it has stored and recalled exactly like I wanted!!

My question is now, how do I use this data to do the math as it is all in a list...?

Code:
NewIngQuant = ingmeasure/people
NewIngMeasure = NewIngQuant * newpeople
Or something...
4. Originally Posted by rrashkin
and retrieve the amount for each with for instance:
Code:
for ingnm in ingredients:
amount=ingredients[ingnm]
Now you can multiply amount*(party_size/recipe_feeds_this_many) and know that it is related to the ingnm ingredient. And since you're using python3 we need not worry ourselves with integer divisions.
5. No Profile Picture
Registered User
Devshed Newbie (0 - 499 posts)

Join Date
Mar 2014
Posts
6
Rep Power
0
Thanks! It works fine for what I need, although not perfectly.

I went with:

Code:
ingredients.keys()

for ingnm in ingredients:
amount=ingredients[ingnm]
print(("%.2f" % (newpeople*(amount/people))),"grams of",ingnm)
Sorry to ask again, but so I know what happened, can someone explain 'dictionary' and what I actually did with:

Code:
ingredients[ingname]=ingmeasure
and

Code:
ingredients.keys()

for ingnm in ingredients:
amount=ingredients[ingnm]
And what even is ingnm and where did it come from?!

Thanks!

Whilst I'm here I'll ask... How would I put another variable from the loop into the dictionary? For example if I wanted measurment not to be static but dynamically entered then recalled at the end in the above code?

6. A python dictionary (dict() instance) is an associative array.

Hashable: usually means immutable, such as numbers and tuples. Strings are, roughly, subsets of tuples.

Hashable objects can be used as dict keys. Values can be any python object. One way to insert additional items into the dictionary uses the notation:

my_dictionary[hashable_key] = value

Python collection objects---common examples include lists, sets, tuples, strings, dictionaries, text files open for reading---are iterable. Thus in the code

for an_object in an_iterable:
process(an_object)

is a loop that runs the indented block of statements below the "for" statement for all the objects returned by an_iterable. Python assigns to the an_object variable the particular object before the block of statements executes.

list, tuple, str iterators return the objects in order, one object or character at a time.
set, dict yield all the values but in an unpredictable order. dict iterates over keys since keys are unique.
text file objects yield a line of text at a time.

5. Data Structures
7. No Profile Picture
Registered User
Devshed Newbie (0 - 499 posts)

Join Date
Mar 2014
Posts
6
Rep Power
0
Hello!

Thanks for the help previously. I now have a working (as good as needed - almost!) recipe calculator.

Please could I have some advice. With 2 variables going through the loop ingname and ingmeasure, I can get them to print at theend of the calcualtor perfectly. I have an issue where I am putting a 3rd variable through - ingtype (which represents the type of measurement, rather than assuming everything will be in grams).

The code below works and shows what I have done, essentially (I believe) creating 2 dictionaries at the same time.

My thought is "surely this can all be done using one dictionary?". I'm just unsure how to write this then recall it later.

Code:
recipename=(input("What is your meal called? "))

ingQuant = int(input("How many ingredients are in this meal? "))

print ("The number of ingredients in",recipename,"is",ingQuant)
people=int(input("How many people will this serve? "))
print ("This recipe will serve",people,"people")
print ('\n')

ingredients={}
measuringtype={}
for loopCounter in range(ingQuant):
print("Ingredient number",loopCounter+1)
ingmeasure=float(input("How much do you need?: "))
ingredients[ingname]=ingmeasure
ingtype=(input("please enter measurment type (eg. grams)"))
measuringtype[ingname]=ingtype

print ('\n')

print ('\n')

print("All ingedients entered, Thank you")

print ('\n')
print("--------------")
print ('\n')
print("To calcualate recipe for",recipename,"for a new number of people...")

newpeople=int(input("What is your new number of people to be served? "))

print ('\n')
print("--------------")
print ('\n')

print("To make",recipename,"for",newpeople,"people, you will need:")

ingredients.keys()

for ingname in ingredients:
print(ingname,("%.2f" % (newpeople*(ingredients[ingname]/people))),measuringtype[ingname])