Python Forum
Why this simple function doesnt work?
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Why this simple function doesnt work?
#1
>>> def correct(s):
    for c in s:
        if c=='1':
            c='I'
        if c=='5':
            c='S'
        if c=='0':
            c='O'
        else:
            c=c
    return s

>>> x = "1teruje5z t0"
>>> correct(x)
'1teruje5z t0'
W T F? Tried many things, this is simply as can be and still doesnt work.
Reply
#2
Quote:doesnt work.
please be more specific, how does it not work?
Reply
#3
the argument you pass is s. you iterate over it performing some operations and at the end you return s unchanged.
If you can't explain it to a six year old, you don't understand it yourself, Albert Einstein
How to Ask Questions The Smart Way: link and another link
Create MCV example
Debug small programs

Reply
#4
Moving the return line one tab forward also wont fix the problem cause it will return it after first char iteration, so what is pythonic way to do this?
Reply
#5
I didn't suggest changing the indentation. You need to store value of c (changed or not) while iterating over s and then construct new string and return it.
and one more advise - the sooner you get rid of habit using single letter cryptic variable names - the better.
If you can't explain it to a six year old, you don't understand it yourself, Albert Einstein
How to Ask Questions The Smart Way: link and another link
Create MCV example
Debug small programs

Reply
#6
Yeah but how to do it without writing a tons of the code? Should be some lambda or 2-3 lines fast and easy way for this.
Reply
#7
def correct(text):
    corrections = {'1':'I', '5':'S'}
    return ''.join(corrections.get(char, char) for char in text)

print(correct("1teruje5z t0"))
Output:
IterujeSz t0
If you can't explain it to a six year old, you don't understand it yourself, Albert Einstein
How to Ask Questions The Smart Way: link and another link
Create MCV example
Debug small programs

Reply
#8
Thats very elegant solution, I will remember this.Cheers
Reply
#9
If it is one-char to one-char translation, you can do even more elegant
def correct(text):
    corrections = str.maketrans('15', 'IS') # or corrections = str.maketrans({'1':'I', '5':'S'})
    return text.translate(corrections)

print(correct("1teruje5z t0"))
this will not work for one-char to many-char-string translation
If you can't explain it to a six year old, you don't understand it yourself, Albert Einstein
How to Ask Questions The Smart Way: link and another link
Create MCV example
Debug small programs

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
  Pydoc documentation doesnt work Cosmosso 5 4,265 Nov-25-2023, 11:17 PM
Last Post: vidito
  pip install requests doesnt work misodca 8 5,604 Jul-07-2023, 08:04 AM
Last Post: zyple
  Ldap3 Python print(conn.entries) doesnt work ilknurg 15 5,573 Dec-28-2022, 11:22 AM
Last Post: shad
  I dont know why my function won't work? MehHz2526 3 1,150 Nov-28-2022, 09:32 PM
Last Post: deanhystad
  pip install pystyle doesnt work person_probably 2 2,004 Sep-23-2022, 02:59 PM
Last Post: person_probably
  Why doesnt chunk generation work? LotosProgramer 1 1,910 Apr-02-2022, 08:25 AM
Last Post: deanhystad
  Join dataframes... So simple but I can't work it out! snakes 1 1,341 Oct-27-2021, 09:15 AM
Last Post: snakes
  time function does not work tester_V 4 2,950 Oct-17-2021, 05:48 PM
Last Post: tester_V
  write new function or change the old one to work "smartter? korenron 3 1,926 Aug-09-2021, 10:36 AM
Last Post: jamesaarr

Forum Jump:

User Panel Messages

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