calories={
'Flour': 364,
'Pepper': 20.1,
Olive: 115,
"Pork": 242.1
}
def Calories (name,kcal):
return calories.get(name)*kcal//100
print (calories ('pork',500))
print (calories ("beef", 500))
Here, the result is that pork needs to be 1210.5 <- decimal point Beef doesn't have a key, so it has to float to None.
in the real life The result is that pork is 1210.0 and beef appears as an error. What's wrong?
python dictionary mapping
>>> None*500
Traceback (most recent call last):
File "<pyshell#14>", line 1, in <module>
None*500
TypeError: unsupported operand type(s) for *: 'NoneType' and 'int'
Let's say the result of get is None, and then 500 times, and then there's an exception.
Use only one when dividing the price of pork. // It's about getting your share.
>>> 242.1 * 500 / 100
1210.5
>>> 242.1 * 500 // 100
1210.0
© 2024 OneMinuteCode. All rights reserved.