1

My homework question is this:

Python allows you to repeat a string by multiplying it by an integer, e.g. 'Hi' * 3 will give 'HiHiHi'. Pretend that this feature does not exist, and instead write a function named repeat that accepts a string and an integer as arguments. The function should return a string of the original string repeated the specified number of times, e.g. repeat('Hi', 3) should return 'HiHiHi'.

And I answered it as following:

def repeat(string,number):
    fstring = ""
    for var in range(1,number+1):
         fstring+=fstring+string
    print(fstring)
repeat("Hi",3)

But the result is not what I had expected: HiHiHiHiHiHiHi . I am not able to see the mistake, help me!

Georgy
  • 12,464
  • 7
  • 65
  • 73

6 Answers6

2

From your code:

         fstring+=fstring+string

This takes whatever fstring already is, plus string and adds that to fstring.

So, fstring goes from '' to '' + '' + 'Hi' which is 'Hi'. And then to 'Hi' + 'Hi' + 'Hi' and finally to 'HiHiHi' + 'HiHiHi' + 'Hi'.

Also, you use for var in range(1,number+1):, but it would be more readable and quicker to use for var in range(0, number):, since you don't really need the value of number to range from 1 to number, you just want it to run number times.

You also don't need var, in that case you can use _ which tells Python to just ignore the actual loop variable name.

Instead of printing at the end, you probably want the function to return the result, so it can be used somewhere (for printing, for example).

Finally, I'd recommend against calling a variable string, because this might get confused with the builtin str. Something like s is a common name for a temporary string.

So, you'd end up with:

def repeat(s, number):
    result = ''
    for _ in range(0, number):
         result += s
    return result

print(repeat("Hi", 3))
Grismar
  • 27,561
  • 4
  • 31
  • 54
1

The following is doubling the previous fstring each time:

fstring+=fstring+string

Since it uses the += operator, it's equivalent to:

fstring = fstring + fstring + string

Just change it to this:

fstring += string

to fix it.

Tom Karzes
  • 22,815
  • 2
  • 22
  • 41
1

The assignment operator += is adding both the string and new string, so you need to use either:

fstring=fstring+string

Or

fstring+=string
Wasif
  • 14,755
  • 3
  • 14
  • 34
  • 1
    I downvoted because I felt your answer added nothing to what the other answers provided previously, some literally what you provided. – Grismar Nov 06 '20 at 03:18
  • 1
    Yes i confess that yours and the other top answer is the fastest and best and deserve upvotes and mine not, but still i was late a minute or two, so in my humble opinion you might remove the downvote (or not if you think, i'll never mind) – Wasif Nov 06 '20 at 03:21
  • I wasn't comparing to my own answer (that goes into detail about some other aspects, so could be considered a bit too broad), but rather @TomKarzes clear, brief and to the point answer, that provides the exact same solution, but was posted 3 minutes earlier. – Grismar Nov 06 '20 at 03:54
0
def repeat(string,number):
    fstring = ""
    for var in range(1,number+1):
         fstring+=string
    return fstring
repeat("Hi",3)

Remember that this function is "Value-Returning Function", if you want to print than use:

print(repeat("Hi",3))
gañañufla
  • 552
  • 2
  • 12
0

I was able to get it to work by removing the +1 in the for loop. the code looks like this

def repeat(string,number):
fstring = ""
for var in range(1,number):
     fstring+=fstring+string
print(fstring)
repeat("Hi",3)
Jax
  • 57
  • 7
  • The code in your answer is same as that of question. –  Nov 06 '20 at 03:29
  • @HiterDean Oh my bad, I must not have copied the code from me editor on accident. – Jax Nov 06 '20 at 03:34
  • This code works only in case of when number=3 but if I choose number=1 or number=4, the code will not work. (https://repl.it/repls/CornyClassicDecagon#main.py). –  Nov 06 '20 at 03:40
0

I think the best way to explain this is to run the iterations manually. Initially, fstring is empty, so you add fstring and string to the fstring variable. The result: "" + "Hi" = "Hi", all good.

Second iteration: fstring = "Hi" string = "Hi" You would think that the result would be "HiHi", but you're accumulating to your fstring variable! So it's:

Original fstring + fstring + string = HiHiHi

Third iteration: The original is already HiHiHi and we add another HiHiHi and we add the original string. So we get seven "Hi"s.

The function will keep adding the fstring to itself, so it will get pretty big, pretty fast.

The solution is to append only your original string to the fstring in the for loop.

Like this:

    fstring = ""
    for var in range(1,number+1):
         fstring+=string
    print(fstring)
repeat("Hi",3)

This way, you're appending only your original string.

SMG-1
  • 11
  • 2