unsupported operand type(s) - Printable Version +- Python Forum (https://python-forum.io) +-- Forum: Python Coding (https://python-forum.io/forum-7.html) +--- Forum: General Coding Help (https://python-forum.io/forum-8.html) +--- Thread: unsupported operand type(s) (/thread-16948.html) |
unsupported operand type(s) - Pythenx - Mar-21-2019 I am making a calculator which calculates distance/speed/velocity with different kinds of units. No problems with the maths itself, but something wrong with the code. it shows TypeError: unsupported operand type(s) for /: 'float' and 'NoneType' (line 7). I tried converting everything what I can to floats, but it keeps popping up. I would appreciate your help very much, thanks! def calculate(unit_time, unit_distance, unit_speed, time, distance, velocity): speed_units = {"km/S": 0.001, "km/min": 0.06, "m/S": 1, "km/h": 3.6, "dm/S": 10, "cm/S": 100, "mm/S": 1000, "dm/min": 600, "cm/min": 6000, "dm/h": 36000, "mm/min": 60000, "cm/h": 360000, "mm/h": 3600000} time_units = {"S": 1, "min": 60, "h": 3600} distance_units = {"mm": 0.001, "cm": 0.01, "dm": 0.1, "m": 1, "km": 1000} if velocity == "x": result = distance / time / (time_units.get(unit_time) / distance_units.get(unit_distance) / speed_units.get(unit_speed)) return result if distance == "x": result = velocity * time / (distance_units.get(unit_distance) / (time_units.get(unit_time) / speed_units.get(unit_speed))) return result if time == "x": result = distance / velocity / (time_units.get(unit_time) / distance_units.get(unit_distance) / speed_units.get(unit_speed)) return result print(calculate("min", "m", "cm/km", 12.0, 13.0, "x")) RE: unsupported operand type(s) - Larz60+ - Mar-21-2019 Some input to your calculation is not what you expect it to be. The only practical way to figure out what that is, is to print each variable out to see what's actually wrong. Also, please post original error complete and unaltered. There is much useful information supplied before actual error occurs. Add after line 5: print(f'distance: {distance}, unit_time: {time_units.get(unit_time)}') print(f'unit_distance: {distance_units.get(unit_distance)}, unit_speed: {speed_units.get(unit_speed)}') RE: unsupported operand type(s) - Pythenx - Mar-21-2019 unit_speed: None - this is the problem. What could I do to fix it? Little help with NoneType - Pythenx - Mar-21-2019 This is a part of a calculator which will take 6 inputs - 2 numbers, 3 units and 1 x. All the maths are working, but there is an error in the code. I tried finding in on the internet, but unsuccessfully. I am desperate, I would appreciate any help very much. Why is the unit_speed NoneType? How do I fix it? def calculate(unit_time, unit_distance, unit_speed, time, distance, velocity): speed_units = {"km/S": 0.001, "km/min": 0.06, "m/S": 1, "km/h": 3.6, "dm/S": 10, "cm/S": 100, "mm/S": 1000, "dm/min": 600, "cm/min": 6000, "dm/h": 36000, "mm/min": 60000, "cm/h": 360000, "mm/h": 3600000} time_units = {"S": 1, "min": 60, "h": 3600} distance_units = {"mm": 0.001, "cm": 0.01, "dm": 0.1, "m": 1, "km": 1000} print(f'distance: {distance}, unit_time: {time_units.get(unit_time)}') print(f'unit_distance: {distance_units.get(unit_distance)}, unit_speed: {speed_units.get(str(unit_speed))}') if velocity == "x": result = distance / time / ( time_units.get(unit_time) / distance_units.get(unit_distance) / speed_units.get(str(unit_speed))) if distance == "x": result = velocity * time / ( distance_units.get(unit_distance) / (time_units.get(unit_time) / speed_units.get(unit_speed))) if time == "x": result = distance / velocity / ( time_units.get(unit_time) / distance_units.get(unit_distance) / speed_units.get(unit_speed)) return result print(calculate("min", "m", "cm/km", 12.0, 13.0, "x"))
RE: Little help with NoneType - nilamo - Mar-21-2019 Please don't remove your post. If someone else has a similar issue, keeping solved issues around helps them in the future. RE: unsupported operand type(s) - Larz60+ - Mar-21-2019 There is no entry 'cm/km' in speed_units, add it, or use correct key. Also proper way to get a dictionary value is dictname[key] So for example (using a valid key): >>> speed_units = {"km/S": 0.001, "km/min": 0.06, "m/S": 1, "km/h": 3.6, "dm/S": 10, "cm/S": 100, "mm/S": 1000, "dm/min": 600, "cm/min": 6000, "dm/h": 36000, "mm/min": 60000, "cm/h": 360000, "mm/h": 3600000} >>> unit_speed = "km/min" >>> speed_units[unit_speed] 0.06 >>>Don't use 'get' that's java not python |