Reputation: 21
How would I get this function to repeat the same characters given from the user on the same line? Instead it prints it separately on each line.
character_to_enter = input('What character would you like to repeat?: ')
character_count = input("How many " + character_to_enter + "'s would you like to enter?: ")
def repeater(character_count):
for repeats in range(int(character_count)):
print(character_to_enter)
repeater(character_count)
The output looks like this with the current code above
What character would you like to repeat?: f
How many f's would you like to enter?: 2
f
f
Process finished with exit code 0
I need it to look like this
What character would you like to repeat?: f
How many f's would you like to enter?: 4
ffff
Process finished with exit code 0
Upvotes: 0
Views: 434
Reputation: 363
One of the easiest and simplest ways to achieve this, is to use the "end" argument of print. Like so:
def repeater(character_count):
for repeats in range(int(character_count)):
print(character_to_enter, end="")
repeater(character_count)
This way instead of ending each print with the default newline character, it doesn't add any character.
Upvotes: 1