dimanche 22 mars 2015

Python: if/elif function that only returns True responses to a new dictionary?

I ask a user a set of questions from a dictionary:



questions = {
"strong": "Do ye like yer drinks strong? ",
"salty": "Do ye like it with a salty tang? ",
"bitter": "Are ye a lubber who likes it bitter? ",
"sweet": "Would ye like a bit of sweetness with yer poison? ",
"fruity": "Are ye one for a fruity finish? "
}


These keys are associated with another dictionary:



ingredients = {
"strong": ["glug of rum", "slug of whiskey", "splash of gin"],
"salty": ["olive on a stick", "salt-dusted rim", "rasher of bacon"],
"bitter": ["shake of bitters", "splash of tonic", "twist of lemon peel"],
"sweet": ["sugar cube", "spoonful of honey", "splash of cola"],
"fruity": ["slice of orange", "dash of cassis", "cherry on top"]
}


and I ask them the questions through a "simple" if/elif set for each question, and assign their answers to a new dictionary:



beverage = {}


def drink():
"""Find out user preferences and assign to new dictionary"""
if raw_input(questions["strong"]) == "Yes" or "yes":
beverage["strong"] = ingredients["strong"]
return True
elif raw_input(questions["strong"]) == "No" or "no":
return False

drink()


And lastly print beverage:



print beverage


BUT, the results have been putting every dictionary key/value into the new beverage dictionary whether I answer "yes" or "no".


How do I only return "Yes" values to the new beverage dictionary?


I believe the error is in my return statements, as I must not understand their execution well enough.


Aucun commentaire:

Enregistrer un commentaire