Making a Purchase: How to check if Banana or Strawbery in the list of food ? compute_bill?


#1

The codeacademy assigment requested
Note that your function should work for any food list ==> which means i can insert "Strawbery" and function should ignore it when it checks the food list and doesn't find it
how can i write such if statement ? ( attached successfull code without such "if" case )

I want to say in the loop indside
for x in food:
if [ the fruit exists in the prices list ] : # price[x] != null
calculate the total
else:
ignore that fruit


#2

There's an operator for that, in

example:

'a' in 'ionatan' # True


#3

i just figured out that
we can use prices.get(x) as a condition , it will be true as the fruit exist in food list.


#4

I will try that function as well . Thnx


#5

both ways works
a in prices_list # True
and prices.get(x) # True

Check attached


#6

get doesn't act as a test if a value is a key in a dictionary. It does something similar but not the same under all circumstances. It's important to write code that does exactly what you want, not approximately the same - that would be asking for bugs.

my_dict = { 'hello': False }

in_result = 'hello' in my_dict
get_result = my_dict.get('hello')

print in_result, get_result  # True False (opposite results)

#7

Thanx for the clarification. Now , It looks obviously different in case of dictionary as you mentioned .

But for my understanding , ( for lists , not dictionaries)
Do you see a difference between list.get() and 'x' in list as both of them returns True if the variable x exists in the list .

I just seek the best programming practice

Thnx


#8

list has no method named get


#9

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.