The code you wrote is a little convoluted I'm afraid. A couple points though:
- Your for loop should not be indented.
- The "result" you calculate in the if-statements is not being used anywhere. I'm not sure this is intentional?
The error you are getting is from this line:
newword =newword+ chr(newword) + offset
newword is an integer prior to this statement, because ord(char)
returns an integer.
Calling chr(newword
turns that into a character... but then you try to add the offset (which is an integer) to that character.
Perhaps what you meant to do on this line is something more like newword = newword + chr(newword + offset)
HOWEVER, this will not yield you a caesar cipher of "word" as you are overwriting your previously shifted characters with the new characters each time you do the assignment here -> newword= ord(char)
Overall, I would perhaps suggest the following changes to your code:
word=input("enter something to encrypt")
offset=int(input("Enter a number to shift it by"))
newword = ""
for char in word:
newchar = ord(char)
newchar = newchar + offset
if newchar < 32:
newchar = newchar + 95
elif newchar > 126:
newchar = newchar - 95
newword = newword + chr(newchar)
print("Encrypted word: " + newword)