2

I am trying to write a short script that will convert a word or sentence into it's alphabetical value, and then jump forward 5 values, and print the result as a string. For e.g.

['a', 'b', 'c']

Should change to...

'102 103 104'

However, I only get the above error. The code in question:

def enc(input, output, seq, str_int):
     input = input.lower()
     output = []
     for char in input:
         num = ord(char) + 5
         str_int = str(num)
         output.append(str_int)
         output = seq.join(output)
     return output
print(enc("hello", [], ' ', ' '))

I'm sure I'm just missing something really obvious. Thanks.

4
  • you are converting output = seq.join(output) output to string that is why the problem occurs Commented Oct 5, 2015 at 7:09
  • Yes, it was meant to be a list originally, but then changed to a string afterwards, to print. Commented Oct 5, 2015 at 7:10
  • 1
    it should indented straight with return statement or you could remove output = seq.join(output) and type return seq.join(output) your problem is due to wrong indentation Commented Oct 5, 2015 at 7:12
  • Ah, yes, that seems to work. Thanks for the help. :) Commented Oct 5, 2015 at 7:13

1 Answer 1

2

The issue occurs because of the line -

output = seq.join(output)

According to the indentation , this is inside the for loop, and hence inside the for loop, you are changing the output variable to str (string) , after that when you try to do output.append() , it errors out. This is the main cause of the issue.

I am guessing you actually only intended to do this outside the loop , after completely creating the output list. But you really don't need to set it back , you can simply do -

def enc(input, output, seq, str_int):
     input = input.lower()
     for char in input:
         num = ord(char) + 5
         str_int = str(num)
         output.append(str_int)
     return seq.join(output)

Demo -

>>> def enc(input, output, seq, str_int):
...      input = input.lower()
...      for char in input:
...          num = ord(char) + 5
...          str_int = str(num)
...          output.append(str_int)
...      return seq.join(output)
...
>>> print(enc("hello", [], ' ', ' '))
109 106 113 113 116
Sign up to request clarification or add additional context in comments.

1 Comment

I realize my mistake now. Thanks again. :) I would accept your answer, but the timer is a thing.

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.