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
  time function does not work tester_V 4 246 Oct-17-2021, 05:48 PM
Last Post: tester_V
  write new function or change the old one to work "smartter? korenron 3 315 Aug-09-2021, 10:36 AM
Last Post: jamesaarr
  string function doesn't work in script ClockPillow 3 556 Jul-13-2021, 02:47 PM
Last Post: deanhystad
  Why doesnt chunk generation work? LotosProgramer 0 283 Jun-18-2021, 01:13 PM
Last Post: LotosProgramer
  if conditions in the for indentation doesnt work ? Sutsro 6 1,440 Jun-15-2021, 11:45 PM
Last Post: bowlofred
  I have two Same Code but One of them Doesnt Work beginner721 6 873 Jan-22-2021, 10:56 PM
Last Post: beginner721
  Why does unpickling only work ouside of a function? pjfarley3 5 1,156 Dec-24-2020, 08:31 AM
Last Post: pjfarley3
  code doesnt return anything ofrihemo 3 846 Jun-30-2020, 05:14 PM
Last Post: ofrihemo
  taking input doesnt print as list bntayfur 2 866 Jun-04-2020, 02:48 AM
Last Post: bntayfur
  len() function, numbers doesn't work with Geany Editor Penguin827 3 1,204 May-08-2020, 04:08 AM
Last Post: buran

Forum Jump:

User Panel Messages

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