0

So here is what I have. This is a program in which I have to take a random string, ex) "]][][sfgfbd[pdsbs]\bdgb"; and strip it of all special characters. the function "Strip" works for its purpose.

message=(str.lower(input("Enter a Coded message: ")))
offset=int(input("Enter Offset: "))
alphabet="abcdefghijklmnopqrstuvwxyz"
def strip(text):
    print("Your Lower case string is: ",message)
    print("With the specials stripped: ")
    for index in text:
        if index in alphabet:
            print(index, end="")
    print()
    return strip

I need the output from strip in the "decode" function, but I can't seem to figure out anyway of storing the iterations of "index"

def decode(character):
    encrypted= ""
    for character in message:
        global offset
        if character == " ":
            encrypted+= " "
        elif ord(character) + offset > ord("z"):
            encrypted+=chr(ord(character) +offset - 26)
        else:
            encrypted+= chr(ord(character)+(offset))
    print("the decoded string is: ",encrypted,end=" ")
    print()

So "decode" only takes the output from the original "message" input. "Palin" however succeeds in taking decode's value.

def palin(code):
    print(code[::-1])
    print(code[:])
    if code[::-1]==code[:]:
        print("This is a Palindrome!")
    else:
        print("This is not a Palindrome.")
    return palin
print()
palin(decode(strip(message)))
1
  • Hello, sorry just to better understand what you are asking, could you possibly give an example of the output you were expecting. Thank you. Commented Oct 7, 2018 at 22:53

1 Answer 1

1

Don't get confused between print and return.

You need to look carefully at the outputs of your methods (what they return, not what they print to the console):

  • the strip() and palin() methods are returning references to themselves, rather than anything useful relating to their inputs.
  • the decode() method isn't returning anything.

To fix this, you can use a variable inside your method, that you build based on the input variables, using the logic you want. For example:

def strip(text):
    print("Your Lower case string is: ",text)
    print("With the specials stripped: ")
    stripped_text = "" # <-- create and initialise a return variable
    for index in text:
        if index in alphabet:
            stripped_text += index # <-- update your return variable
            print(index, end="")
    print()
    return stripped_text # <-- return the updated variable

You then need to do something similar for decode(), although here you already have an output variable (encrypted) so you just need to return it at the end of the method.

The palin() method doesn't need to return anything: it just prints out the result.


Once you get this working, you should think about how you can use other features of the Python language to achieve your goals more easily.

For example, you can use replace() to simplify your strip() method:

def strip(text):
    return text.replace('[^a-z ]','') # <-- that's all you need :)
Sign up to request clarification or add additional context in comments.

3 Comments

do I have to make stripped_text global to input it into decode?
No, decode(strip(message)) says ‘call decode() using the output from strip()’ so whatever strip() returns will be used as the input for decode(). But for that to work, you need to fix the decode() method, to be def decode(message):
... def decode(character): is wrong - the value of the character input is being ignored

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.