0
s=cs50.get_string()
luhn=0
n=len(s)
for i in range(-1,-n-1,-1):
    if i%2==0:
        if 2*ord(s[i])>9:
            luhn+=2*ord(s[i])-9
        else:
            luhn+=2*ord(s[i])

    else:
        luhn+=ord(s[i])

print("{}".format(ord(s[i])))


if luhn%10==0:
    print("Valid")
else:
    print("invalid")

When 54 is passed as 's'. I get ord(s[i]) as 53. Why is that?

1

Suggest you use the 378282246310005 example from the spec and print the value of luhn in your output. You know the answer should be 60. But it won't be because .......(hover for spoiler)

You are only adding the last value of clow to luhn. Then ask yourself: Do I really need clow?

2
  • I excluded clow. And yes, luhn was coming out totally wrong So then i tried printing out ord(s[i]) when s= "54" which gives me 53, when i is -2 i.e pointing to '5' of the '54'. Isn't ord() supposed to print out just the integer form of the char in a string?
    – Vaish MK
    Feb 24 '17 at 6:27
  • Got it. int() was the thing to use not ord()
    – Vaish MK
    Feb 24 '17 at 10:54

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .