0

I'm trying to save the result of an addition in JSON format. I want as output something like that:

Result_0=[add,1,1,2]

but instead I have this result:

{Result_0=["add",1,1,null]}

I don't get why it read the result of the operation as null value. This is my code:

from Exercise_0 import *
import json

if __name__=="__main__":
    c= Calculator('Casio')
    d={}
    a=1
    b=1
    result=c.add(a,b)
    key="Result_0"
    d[key]=["add",1,1,result]
    print(json.dumps(d))

Exercise_0:

class Calculator():
    def __init__(self, name):
        self.name=name


    def add(self, number_1, number_2):
        print(f"{number_1}+{number_2}={number_1+number_2}")


    def sub(self, number_1,number_2):
        print(f"{number_1}-{number_2}={number_1-number_2}")

    def mul(self, number_1,number_2):
        print(f"{number_1}*{number_2}={number_1*number_2}")

    def div(self, number_1,number_2):   
        print(f"{number_1}/{number_2}={number_1/number_2}")
RadioTune
  • 21
  • 5
  • just saving in json format and displaying it as json output. No particular purpouse. – RadioTune Feb 16 '20 at 17:24
  • You mean _encode_? There's no encryption here. – ChrisGPT was on strike Feb 16 '20 at 17:24
  • It seems perfectly normal JSON output – those curly brackets are part of it. What has JSON to do with encryption? Are you sure you aren't looking for serialization instead? – Jongware Feb 16 '20 at 17:24
  • Can you print out `result` just after when you calculate it? – denis_lor Feb 16 '20 at 17:27
  • 2
    There's your problem. `add()` (and all of your other operations) don't `return` anything. Hence `None` in Python, which becomes `null` in JavaScript. Replace `print(foo)` with `return foo`. – ChrisGPT was on strike Feb 16 '20 at 17:27
  • For usr2564301: Maybe it's serialization. The point is that I can't save the result of the operation as a number. It keeps saving it as a null value in json and I don't know what I'm doing wrong, either conceptually or practically – RadioTune Feb 16 '20 at 17:28
  • 1
    Like I said, you're not `return`ing anything. That's what you're doing wrong. – ChrisGPT was on strike Feb 16 '20 at 17:29
  • Chris, thank you! I didn't know about this, now I feel dumb ahah I tried to look on here for some similar question but not understanding the problem I couldn't find anything. Thank you again! Now it works – RadioTune Feb 16 '20 at 17:31
  • `import *` is bad practice. – AMC Feb 16 '20 at 21:34
  • 1
    https://ericlippert.com/2014/03/05/how-to-debug-small-programs/ – AMC Feb 16 '20 at 21:34

1 Answers1

0

Change add() into:

def add(self, number_1, number_2):
        return number_1+number_2

And you will get:

{"Result_0": ["add", 1, 1, 2]}

You should do the same for other functions as sub, mul... as well, so the result is returned back to the result variable which is later on used with the value of the function result. Something like:

import json

class Calculator():
    def __init__(self, name):
        self.name=name

    def add(self, number_1, number_2):
        return number_1+number_2

    def sub(self, number_1, number_2):
        return number_1-number_2

    def mul(self, number_1, number_2):
        return number_1*number_2

    def div(self, number_1, number_2):   
        return number_1/number_2


if __name__=="__main__":
    c= Calculator('Casio')
    d={}
    a=3
    b=2

    key="Result_0"
    result=c.add(a,b)
    d[key]=["add",3,2,result]
    print(json.dumps(d))

    result=c.sub(a,b)
    d[key]=["sub",3,2,result]
    print(json.dumps(d))

    result=c.mul(a,b)
    d[key]=["mul",3,2,result]
    print(json.dumps(d))

    result=c.div(a,b)
    d[key]=["div",3,2,result]
    print(json.dumps(d))

Resulting in:

{"Result_0": ["add", 3, 2, 5]}
{"Result_0": ["sub", 3, 2, 1]}
{"Result_0": ["mul", 3, 2, 6]}
{"Result_0": ["div", 3, 2, 1.5]}
denis_lor
  • 6,212
  • 4
  • 31
  • 55