0
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?

2
  • Also, if you output in every error situation the same message Invalid ID, you are not seeing at which place the checking fails. This makes it unnecessarily difficult to debug. Either use different error messages in each case, or differentiate the cases via the exit code. Your program now returns exit code 0 even in the error case, which is not a good idea anyway. Commented Nov 25, 2021 at 9:51
  • what is wrong with good old debugging, we did not have SO 20 years back and we where able to find and fix problems Commented Nov 25, 2021 at 10:48

1 Answer 1

1

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.

Sign up to request clarification or add additional context in comments.

Comments

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.