#๐ JSON
26 messages ยท Page 1 of 1 (latest)
@fresh elbow
Remember to:
- Ask your Python question, not if you can ask or if there's an expert who can help.
- Show a code sample as text (rather than a screenshot) and the error message, if you've got one.
- Explain what you expect to happen and what actually happens.
:warning: Do not pip install anything that isn't related to your question, especially if asked to over DMs.
does the if statement evaluate?
Please react with โ
to upload your file(s) to our paste bin, which is more accessible for some users.
!paste
If your code is too long to fit in a codeblock in Discord, you can paste your code here:
https://paste.pythondiscord.com/
After pasting your code, save it by clicking the Paste! button in the bottom left, or by pressing CTRL + S. After doing that, you will be navigated to the new paste's page. Copy the URL and post it here so others can see it.
paste here
isn't user = 1 here in the example u input?
while in if check ur checking for user == 2
!e
import json
user = 2
class Car:
#Defining the class car that takes paramters\
#registration number, year of production, passenger , vehicle mass
def __init__(self,number, year, passenger, mass):
self.registration_number= number
self.year_of_production= year
self.passenger = passenger
self.vehicle_mass= mass
def decod(obj):
return Car(obj['registration_number'], obj['year_of_production'],obj['passenger'],obj['vehicle_mass'] )
#create new car object
MyCar=Car(number="ABC1234",year=2024,passenger=True, mass=19432 )
#produce a json string descring the vehicle
try:
json_str= json.dumps(MyCar, default= lambda o: o.__dict__)
except TypeError:
print(f'Object {MyCar.__class__.__name__} is not serializable')
else:
print(f'The resulting JSON string is :\n {json_str}')
if user ==2:
json_decoded= json.loads(json_str, object_hook = decod)
print(json_decoded.__dict__)
:white_check_mark: Your 3.12 eval job has completed with return code 0.
001 | The resulting JSON string is :
002 | {"registration_number": "ABC1234", "year_of_production": 2024, "passenger": true, "vehicle_mass": 19432}
003 | {'registration_number': 'ABC1234', 'year_of_production': 2024, 'passenger': True, 'vehicle_mass': 19432}
ok
could you please be more explicity if you don't mind ? Thanks you
in ur case
if user == 2 was false
u set user to 1 in ur input
so it wasn't evaluating at all
and in decod function u had obj['mass'] which is wrong it should be obj['vehicle_mass']
thANKS A LOT
This help channel has been closed. Feel free to create a new post in #1035199133436354600. To maximize your chances of getting a response, check out this guide on asking good questions.