Home > Software engineering >  My python code output in VSCode is different from the output I recieve when running my code in my ma
My python code output in VSCode is different from the output I recieve when running my code in my ma

Time:11-26

sum = 0
codeid = input("Please enter your ID code: ")

if len(str(codeid)) == 10:
    sum = sum   int(str(codeid) [0]) * 10
    sum = sum   int(str(codeid) [1]) * 9
    sum = sum   int(str(codeid) [2]) * 8
    sum = sum   int(str(codeid) [3]) * 7
    sum = sum   int(str(codeid) [4]) * 6
    sum = sum   int(str(codeid) [5]) * 5
    sum = sum   int(str(codeid) [6]) * 4
    sum = sum   int(str(codeid) [7]) * 3
    sum = sum   int(str(codeid) [8]) * 2
    remainder = sum % 11
    if remainder >= 2 and (11 - remainder == int(str(codeid) [9])):
        print ("valid ID")
    elif remainder < 2 and (remainder == int(str(codeid) [9])):
        print ("valid ID")
    else : 
        print ("Invalid ID")          
else: 
print ("Invalid ID")

So this is a simple code I've created for detecting either a specific type of ID is valid or not. for example the number "0462519449" is valid due to the algorithm and the output returns "valid" when I run the code in VSCode; however, when I save the program and run it from terminal, I get the output "invalid ID" which is not true since it must be valid. Does anyone know what the problem is?

CodePudding user response:

I think that in the terminal you're using Python 2.

In Python 3, input returns a string (so all your str(codeid) calls are unnecessary).

In Python 2, input evaluates whatever text it's given, so if you type in 0462519449, it will return an int. Specifically, it will return the int 462519449, since the leading zero has no effect. Then your check len(str(codeid)) == 10 is false, which is why you get to "Invalid ID".

If you're running Python 2, you need to use raw_input instead of input.

  • Related