I converted my C code into Python, but keep getting this error: ValueError: invalid literal for int() with base 10.

This is what my terminal is returning:

$ python caesar.py 3
Traceback (most recent call last):
  File "caesar.py", line 24, in <module>
    userMessage[i] = (((int(userMessage[i]) - ord('a')) + key) % 26) + ord('a') 
ValueError: invalid literal for int() with base 10: 'h'

What am I doing wrong? Is it the int() or the ord() or both?

I appreciate the help, guys. Thanks.

2 Answers 2


The message is telling you that 'h' cannot be changed to an int. True enough.

From the Hints section of the spec:

You might find chr and/or ord of help.

I think chr is what you want, not int.

  • Thank you for the reply. I tried your suggestion and now I see another error: 'userMessage[i] = (((chr(userMessage[i]) - 97) + key) % 26) + 97 TypeError: an integer is required (got type str)' I know it's a simple thing that's wrong, I just can't understand it. Can you please take a look at the rest of this section of my code? I really need help. [link]gist.github.com/3bb9d5f620008bae94a606e648382667
    – Lele
    Mar 30, 2017 at 14:50
  • I figured it out! It turned out I needed both chr() and ord(), instead of int(). chr((((ord(userMessage[i])... Thank you for you insight. It was great! :)
    – Lele
    Mar 30, 2017 at 15:36

I got it. wrap i in ord() and the whole cipher calculation for i in chr() :)

You must log in to answer this question.

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