0

enter image description here

Hello, so my task is shown above in the image. I'm not asking for answers, I would just like to know how to get started on this.
My initial ideas are to:
1. Have user input str("example word")
2. Have user input int("Example number")
3. Use a for loop to read the number and then print the word out.

So far, my code is as shown below:

def repeat():
    word=str(input("Please input a single word: "))
    number=int(input("Please input a number: "))
    for i in range(number):
        number+=1
        print(word,end=" ",sep='')
repeat()

However I'm running into two issues:
1. When printing the word out, the output is i.e "hello hello hello" instead of "hellohellohello"
2. I feel like I'm not exactly hitting the right points for the question.

Would appreciate any help!

7
  • 4
    end=" " is adding the space. Commented Oct 9, 2019 at 4:27
  • 3
    Note that your requirement says the function should return a string... even if you correct your print statement to display a result correctly... your function doesn't actually return anything... Commented Oct 9, 2019 at 4:29
  • to rectify that.. should i just put a return statement in? Commented Oct 9, 2019 at 4:32
  • Your solution is fine except that the question asks for word and number to be passed as parameters to repeat instead of input by the user and for the output to be returned rather than printed. You could create an empty string s and then do s += word in the loop instead. Also number+=1 is not needed and doesn't do anything useful. Commented Oct 9, 2019 at 4:34
  • Also the statement says 'that accepts a string and an integer as arguments`, so I'd say using inputs also goes against the requirements. Commented Oct 9, 2019 at 4:36

3 Answers 3

1

This portion of your code:

print(word, end=' ', sep='') 

is adding those spaces. You don't need those. Also, I'm not sure why you're incrementing the 'number' datatype. No need to do that since you're only using that for the amount of times the for loop will go based on the user input. Also, this should all be passed to a function that has two paramters: One to accept and integer and the other to accept a string. For example:

repeat(intA, strB)

Also, my suggestion would be to concatenate. Add your strings together instead of just displaying it multiple times. This will also allow you to create a new variable that will later be returned to the function that called it.

Sign up to request clarification or add additional context in comments.

Comments

0

You can create function as below to repeat the string:

def repeat(text, occurrence):
    new_text = ''
    for i in range(occurrence):
        new_text += text
    return new_text

print(repeat('Hi', 4))  # sample usage

Finally you can implement your code like:

In [6]: repeat(input("Please input a single word: "), int(input("Please input a number: ")))
Please input a single word: hello
Please input a number: 5
Out[6]: 'hellohellohellohellohello'

3 Comments

i see - thank you! why did you not call the repeat function in the end? or is that not necessary?
That would be approach to recursive solution not iterative solution. as the occurrence is fixed and known in prior I used iterative solution
just one more question: what is the purpose of this line? new_text = '''
0

More pythonic is to use a generator expression:

def f(s,n):
    return ''.join(s for _ in range(n))

or Python's standard library:

import itertools as it
def f(s, n):
    return ''.join(it.repeat(s, n))

print(f('Hi', 3))

Both produce

'HiHiHi'

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.