-2

I want to write this recursively to encode other letters in the string with ord and chr, but when I write it recursively it calls for raw_input from user again. How do i fix this?

def rawEncode(input):
    input= raw_input("GIVE ME SUPER SECRET MESSAGE TO ENCODE")
    unencoded="%s" %input [:]
    if "%s" %input =='':
        return ''
    else:
        answer=ord("%s" %input [0])
        return answer
Alexander
  • 105,104
  • 32
  • 201
  • 196

3 Answers3

0

It's better to get input and encode string in separate places. However you can get desired recursion by adding additional flag which disable input forcing and modifying return values to be a list for further concatting of ords:

def rawEncode(input='', force_input=True):
    if force_input:
        input= raw_input("GIVE ME SUPER SECRET MESSAGE TO ENCODE")
    if "%s" % input == '':
        return ['']
    else:
        answer = ord("%s" %input [0])
        return [answer] + rawEncode(input=input[1:], force_input=False)

print rawEncode()
Andriy Ivaneyko
  • 20,639
  • 6
  • 60
  • 82
  • From a pure design viewpoint I think it'd make more sense to have the function just take the input (as a required parameter) and have the caller put it in. – Matti Virkkunen Feb 27 '16 at 17:27
0

You can simply put the statement
input= raw_input("GIVE ME SUPER SECRET MESSAGE TO ENCODE")
before the function call.

XZ6H
  • 1,779
  • 19
  • 25
0

it is very simple

TempVar = 0

def rawEncode(input):

    if TempVar == 0:
        input = raw_input("GIVE ME SUPER SECRET MESSAGE TO ENCODE")
        TempVar = TempVar + 1

    unencoded = "%s" % input[:]
    if "%s" % input == '':
        return ''
    else:
        answer = ord("%s" % input[0])
        return answer
ekhumoro
  • 115,249
  • 20
  • 229
  • 336
hb X
  • 69
  • 5