Drinks

Hey if you want to learn mixtures or some datastructures with python you are in right place here.
jack

Cola mit Rum/Whisky/Bier

Inhalt:

  • CocaCola
  • Whisky oder Rum
  • Eiswürfel

Rezeptur A:

1/4 Sprit 3/4 Cola

Rezeptur B:

1/3 Cola 2/3 Bier

Tee auf Englische Art

Inhalt:

  • Ceylon Tee
  • Milch
  • Zuckerwürfel

Rezeptur:

Schwarzen Tee min. 5-8 unter kochendem Wasser ziehen lassen
Milch hinzufügen 2 EL, optional mit einem Zuckerwürfel

This would look in python code like this:

#initialize a list and just add by each step a new item

liquids=[]
>>> liquids.append(“cola”)
>>> liquids.append(“rum”)
>>> liquids.append(“whisky”)
>>> liquids.append(“milk”)

#display the list
>>>liquids
[‘cola’, ‘rum’, ‘whisky’, ‘milk’]

#define a simple method
>>> def list_all(list):
…              for item in list:
…                      print item

#call this new method on the object
>>> list_all(liquids)
cola
rum
whisky
milk

#it is known that rum and whisky is on 2nd and 3rd position
#with this knowledge a new list can be produced by slicing the previous list

sprit=liquids[1:3]

>>>sprit
[‘rum’, ‘whisky’]

#items from the sprit list can be selected randomly (sampling):
#the first argument is the range element to sample from, the second is how many
#items it should sample – interested only in one sample each time

>>> import random
>>> random.sample(sprit,1)
[‘rum’]
>>> random.sample(sprit,1)
[‘whisky’]
>>> random.sample(sprit,1)
[‘rum’]
>>> random.sample(sprit,1)
[‘rum’]

#let’s create a new recept dictionary

#keys are the parts of the individual recept:
>>> recept = {“onethird”: [liquids[0]], “twothird”: random.sample(sprit,1)}

>>> recept

{‘twothird’: [‘whisky’], ‘onethird’: [‘cola’]}

#notice that dictionaries are unsorted by status quo

#however can be #accessed by keys, values or iterators

More questions about this code?

Advertisements