Python Forum
Not quite getting the correct Output from a function
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Not quite getting the correct Output from a function
#1
Hello.

I have a function that I'm trying to create.
When receiving a non-empty string input, I want to return a string containing every other character 2n+2 times.
(Where "n" is the 0-based index of the letter).

Example: input_string = "Rivers" #RRvvvvvvrrrrrrrrrr

def string_expansion( input_string ):
    n=0
    if len(input_string) == 0:
        return "invalid entry"
    #for n in range(len(input_string)):
    #    value = input_string[n::2]
    #    return value * (2*n+2)
    for n in range(0,len(input_string),2):
        #value = input_string[n]
        #strlen = len(input_string)
        print(value * (2*n+2))
    pass

input_string = "Rivers"
print(string_expansion(input_string))
I am getting:

Output:
RR vvvvvv rrrrrrrrrr None
I want to get:
Output:
RRvvvvvvrrrrrrrrrr
What am I goofing up here?
Reply
#2
Your code is printing the value per loop and not returning anything.
You need a container for your string before the loop, add to it inside the loop and return it after the loop.
Reply
#3
If function doesn't return anything explicitly with return statement then it returns None (you can see it in your output: three lines printed and then None). In case of empty string function returns 'invalid entry' therefore there will be no None.

I would go for oneliner:

>>> s = 'Rivers'
>>> ''.join(char*(2*(i-1)+2) for i, char in enumerate(s, start=1) if i % 2)
'RRvvvvvvrrrrrrrrrr'
I'm not 'in'-sane. Indeed, I am so far 'out' of sane that you appear a tiny blip on the distant coast of sanity. Bucky Katt, Get Fuzzy

Da Bishop: There's a dead bishop on the landing. I don't know who keeps bringing them in here. ....but society is to blame.
Reply
#4
Thank you for the replies. I tried both recommendations and they worked wonderfully...and I learned two different approaches in the process. Much appreciated!
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  problem in output of a function akbarza 9 1,178 Sep-29-2023, 11:13 AM
Last Post: snippsat
  How to print the output of a defined function bshoushtarian 4 1,279 Sep-08-2022, 01:44 PM
Last Post: deanhystad
  The formula in the function is not giving the correct value quest 1 1,237 Mar-30-2022, 03:20 AM
Last Post: quest
  output correction using print() function afefDXCTN 3 11,074 Sep-18-2021, 06:57 PM
Last Post: Sky_Mx
  python prints none in function output chairmanme0wme0w 3 2,207 Jul-07-2021, 05:18 PM
Last Post: deanhystad
  print function output wrong with strings. mposwal 5 3,109 Feb-12-2021, 09:04 AM
Last Post: DPaul
  Four sequential bytes; Need to remove high order bit in each to get correct output GrandSean 5 2,931 Feb-06-2021, 07:17 PM
Last Post: GrandSean
  Output with none, print(x) in function Vidar567 3 2,503 Nov-24-2020, 05:40 PM
Last Post: deanhystad
  attribute error instead of correct output MaartenRo 2 2,184 Aug-28-2020, 10:22 AM
Last Post: Larz60+
  How to append to list a function output? rama27 5 6,719 Aug-24-2020, 10:53 AM
Last Post: DeaD_EyE

Forum Jump:

User Panel Messages

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