My question is, how can I approximate the values as follows?
You have to write the code explicitly.
'name'= DD8EFC (Always deleting the first three lines)
Fetch the string, then slice it:
name = plane.get('name')[3:]
print(f"'name' = {name}'")
However, the fact that you're using get
rather than []
implies that you're expecting to handle the possibility that name
doesn't exist in plane
.
If that isn't a possibility, you should just use []
:
name = plane['name'][3:]
If it is, you'll need to provide a default that can be sliced:
name = plane.get('name', '')[3:]
'speed'= 136. (Approximate whole)
It looks like you want to round to 0 fractional digits, but keep it a float
? Call round
with 0
digits on it. And again, either you don't need get
, or you need a different default:
speed = round(plane['speed'], 0)
… or:
speed = round(plane.get('speed', 0.0), 0)
As for printing it: Python doesn't like to print a .
after a float
without also printing any fractional values. You can monkey with format
fields, but it's probably simpler to just put the .
in manually:
print(f"'speed': {speed}.")