Python mapping basic questions

Asked 2 years ago, Updated 2 years ago, 158 views

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

2022-09-22 12:18

2 Answers

>>> 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.


2022-09-22 12:18

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


2022-09-22 12:18

If you have any answers or tips


© 2024 OneMinuteCode. All rights reserved.