-1

I am trying to make a data decoder if you don't know what that is it's basically a code that sees words or sentences from an ".txt" file and decodes it as a Yes or a No.

The code i Made:

def Load_Data(input):
    true = 1
    false = 0
    
    try:
        with open(input,'r') as read_obj:
            for line in read_obj:
                if true or false in line:
                    if line is true:
                        print("True")
                        
                    elif line is false:
                        print("False")
                    
                    
                    else:
                        print("Unable To decode it")
                        break

    
    
    
    except:
           print("Error In code")
           
           

Load_Data('Load.txt')

No Errors But the only thing on screen is Unable To decode it.

any Reasons Why?

The input in the .txt file:

The input i Gave it:

1

0

1

0

1

1

1

it should say: True

False

True

False

True

True

True

Capt.Pyrite
  • 851
  • 7
  • 24

2 Answers2

2

You should know the difference between is and ==. Moreover, there is extra '\n' character for every new line, so you need to filter them and convert string to int.

def Load_Data(input):
true = 1
false = 0

try:
    with open(input,'r') as read_obj:
        for line in read_obj:
            line = int(line.split('\n')[0])
            if true==line or false== line:
                if line == true:
                    print("True")

                elif line ==false:
                    print("False")
                else:
                    print("Unable To decode it")
                    break




except:
       print("Error In code")
       
Load_Data('Load.txt')

Load.txt:

1
0
1
0
1
1
1

Output:

True
False
True
False
True
True
True
ashraful16
  • 2,742
  • 3
  • 11
  • 32
2

You are comparing different types in your code, It won't work if true and false variable are decimal, you should declare them as characters. Maybe you also move out your else statement to have the right behavior. Then check that your input file doesn't have strange char. This is the code:

def Load_Data(input):
    true = '1'
    false = '0'
    
    try:
        with open(input,'r') as read_obj:
            for line in read_obj.read():
                if true or false in line:
                    if line == true:
                        print("True")
                        
                    elif line == false:
                        print("False")
                    
                    
                else:
                    print("Unable To decode it")
                    break

    
    
    
    except:
           print("Error In code")
           
           

Load_Data('Load.txt')
Federico A.
  • 256
  • 2
  • 8