Python Forum
Why won't this user created function work?
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Why won't this user created function work?
#1
This is the code I wrote, but when it prints the message at the end, it prints the original input as if it had not gone through the string function. What's the problem?



message=str(raw_input("Enter the encoded string: "))
message=message.lower()

def strip(string):
    stripped_message=""
    for i in range(0,len(string)):
        character=ord(string[i:i+1])
        if character in range(97,123):
            stripped_message+=chr(character)
    return stripped_message


strip(message)
print message
Reply
#2
your strip() function wont modify the argument, shouldve printed the return value instead
swallow osama bin laden
Reply
#3
It seems, to me, what you're doing is assigning a value to message variable, then in strip() you are a assigning stripped_message an 'altered' value of string and returning it, but not printing stripped_message.

You are executing strip(),without printing anything inside the function. Your last line is just a print function of the original message variable...

I hope the snippet below does the trick for you (if it does pls notice that print() is inside strip()). Think Think

message=str(raw_input("Enter the encoded string: "))
message=message.lower()
 
def strip(string):
    stripped_message=""
    for i in range(0,len(string)):
        character=ord(string[i:i+1])
        if character in range(97,123):
            stripped_message+=chr(character)
    print(stripped_message)#notice where print() occurs
    return stripped_message 

strip(message)
best of luck Smile
Reply
#4
Strings are immutable. If you make changes to one, you need to assign the new string to the variable you want to refer to it by.
Reply
#5
He does it with stripped_message += chr(character), which makes a new string.
I prefer this way:

import string


def to_ascii(text):
    allowed = string.ascii_letters + string.digits
    # change allowed to 
    return ''.join(c for c in text if c in allowed)

print(filter_non_ascii('Hello World. 1243!!!!!........===??????'))
The thing inside the join method, is a Generator Expression.
Almost dead, but too lazy to die: https://sourceserver.info
All humans together. We don't need politicians!
Reply
#6
''.join(filter(lambda ch: ch in allowed, text))
will do too.
"As they say in Mexico 'dosvidaniya'. That makes two vidaniyas."
https://freedns.afraid.org
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  print doesnt work in a function ony 2 233 Mar-11-2024, 12:42 PM
Last Post: Pedroski55
Information How to take url in telegram bot user input and put it as an argument in a function? askfriends 0 1,026 Dec-25-2022, 03:00 PM
Last Post: askfriends
  I dont know why my function won't work? MehHz2526 3 1,149 Nov-28-2022, 09:32 PM
Last Post: deanhystad
  User-defined function to reset variables? Mark17 3 1,588 May-25-2022, 07:22 PM
Last Post: Gribouillis
  Definitions in User-Created Functions and For Loops new_coder_231013 6 2,027 Dec-29-2021, 05:51 AM
Last Post: ndc85430
  time function does not work tester_V 4 2,946 Oct-17-2021, 05:48 PM
Last Post: tester_V
  Exit function from nested function based on user input Turtle 5 2,858 Oct-10-2021, 12:55 AM
Last Post: Turtle
  write new function or change the old one to work "smartter? korenron 3 1,925 Aug-09-2021, 10:36 AM
Last Post: jamesaarr
  string function doesn't work in script ClockPillow 3 2,331 Jul-13-2021, 02:47 PM
Last Post: deanhystad
  Why does unpickling only work ouside of a function? pjfarley3 5 3,363 Dec-24-2020, 08:31 AM
Last Post: pjfarley3

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020