0

I am giving an input, that gets stored in the variable "inp". When I compare it in the if condition shown below, I am not getting the right output. The for loop is executed, but no output is shown. When I replace the variable by string "bin_A", I am getting the right output.

Here is the snippet of my code:

def main():
    with open("apc.json") as data_file:
      data = json.load(data_file)

    inp = raw_input("enter name") 
    print "read the input", str(inp)   
    for i in range(0,12):

      if data["work_order"][i]["bin"] == inp :  #have tried repr(inp)
          print data["work_order"][i]["bin"]
          print "gotcha", i
          print data["work_order"][i]["item"]

if __name__ == '__main__':
    main()

Note: Output of print data["work_order"][i]["bin"] is Bin_A. So, when I try to give my input as Bin_A, nothing happens

2
  • Maybe you should show what is in data[work_order][i][bin]? Commented Jan 9, 2016 at 22:55
  • 1
    what is the type of data data["work_order"][i]["bin"]?? if is not str, then they never will be equals., Also try str(data[...]) given that the print is as you say... Commented Jan 9, 2016 at 23:48

1 Answer 1

2

Considering data was set from

data = json.load(data_file)

Then you probably need to convert the variable i from an int to a str

if data["work_order"][str(i)]["bin"] == inp

Or since you seem to use i multiple times, you could directly say

for i in map(str, range(0,12)):
Sign up to request clarification or add additional context in comments.

2 Comments

conversion [str(i)] doesn't seem to work as well. But, could you please explain the reason for conversion ?
Perhaps str(data["work_order"][i]["bin"]), if otherwise an object is returned?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.