[Python] Python TypeError when adding ord(c) + num in Caesar cipher implementation on Coderbyte

Discussão em 'Python' iniciado por Stack, Julho 7, 2025.

  1. Stack

    Stack Membro Participativo

    TypeError: unsupported operand type(s) for +: 'int' and 'str' in Caesar Cipher Implementation


    I'm working on a Caesar cipher problem on Coderbyte for a fintech SWE position and encountering a TypeError that I can't resolve.

    Problem


    Getting TypeError: unsupported operand type(s) for +: 'int' and 'str' on line 8 where I'm trying to add ord(c) + num.

    Code


    def StringChallenge(strParam, num):
    # code goes here
    results = ""

    for c in strParam:
    if c.isalpha():
    shifted_ord = ord(c) + num # ERROR ON THIS LINE
    if "A" <= c <= "Z":
    if shifted_ord > ord("Z"):
    shifted_ord = shifted_ord - 26
    elif "a" <= c <= "z":
    if shifted_ord > ord("z"):
    shifted_ord = shifted_ord - 26
    results += chr(shifted_ord)
    else:
    results += c

    challengeToken = "ngJ92ipqb70"
    results += challengeToken

    updated = ""
    count = 1
    for i in results:
    if count == 4:
    updated += "_"
    count = 1
    else:
    updated += i
    count += 1

    return updated

    # keep this function call here
    print StringChallenge("Hello", 4)
    print StringChallenge(raw_input())

    Error Message


    Traceback (most recent call last):
    File "/tmp/422136455/main.py", line 41, in <module>
    print StringChallenge("Argument goes here...", "Argument goes here...")
    File "/tmp/422136455/main.py", line 8, in StringChallenge
    shifted_ord = ord(c) + num
    TypeError: unsupported operand type(s) for +: 'int' and 'str'

    What I've Verified

    • strParam is confirmed to be a string type
    • num is confirmed to be an integer type
    • ord(c) returns an integer
    • The error occurs specifically on the addition operation
    Question


    Since ord(c) returns an int and num is an int, the addition ord(c) + num should work. What could be causing this TypeError?

    Continue reading...

Compartilhe esta Página