Python Forum
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
unsupported operand type(s)
#1
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"))
Reply
#2
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)}')
Reply
#3
unit_speed: None - this is the problem. What could I do to fix it?
Reply
#4
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"))
Output:
distance: 13.0, unit_time: 60 unit_distance: 1, unit_speed: None Process finished with exit code 1
Error:
Traceback (most recent call last): File "C:/Users/X/PycharmProjects/sagasgfdras/sagaf.py", line 22, in <module> print(calculate("min", "m", "cm/km", 12.0, 13.0, "x")) File "C:/Users/X/PycharmProjects/sagasgfdras/sagaf.py", line 10, in calculate time_units.get(unit_time) / distance_units.get(unit_distance) / speed_units.get(str(unit_speed))) TypeError: unsupported operand type(s) for /: 'float' and 'NoneType'
Reply
#5
Please don't remove your post. If someone else has a similar issue, keeping solved issues around helps them in the future.
Reply
#6
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
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Type Error: Unsupported Operand jhancock 2 1,153 Jul-22-2023, 11:33 PM
Last Post: jhancock
  TypeError: unsupported operand type(s) for +: 'dict' and 'int' nick12341234 1 9,279 Jul-15-2022, 04:04 AM
Last Post: ndc85430
  TypeError: unsupported opperand type(s) for %: 'int' and 'list' cool_person 7 2,138 May-07-2022, 08:40 AM
Last Post: ibreeden
  unsupported operand type(s) for %: 'list' and 'int' RandomCoder 4 32,823 May-07-2022, 08:07 AM
Last Post: menator01
  unsupported operand type(s) for /: 'str' and 'int' Error for boxplot soft 1 3,047 Feb-09-2021, 05:40 PM
Last Post: soft
  calculate_bai -- TypeError: unsupported operand type(s) for *: 'float' and 'NoneType' pantherd 1 3,234 Apr-21-2020, 12:31 PM
Last Post: anbu23
  Type hinting - return type based on parameter micseydel 2 2,455 Jan-14-2020, 01:20 AM
Last Post: micseydel
  Need help with "unsupported operand type(s)" _dave 6 5,332 Oct-14-2019, 06:43 AM
Last Post: buran
  TypeError: unsupported operand type(s) for -: 'str' and 'str' Balaji 4 10,063 Oct-11-2019, 10:29 AM
Last Post: buran
  TypeError: unsupported operand type(s) for -: 'datetime.datetime' and 'str' findbikash 2 9,579 Sep-18-2019, 08:32 AM
Last Post: buran

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020