How can I make my code repeat an action five times?

0
2
Asked By CuriousCoder91 On

I'm trying to get my code to say an action five times in a row. Here's what I have so far:

```python
people = input("People: ")
action = input("Action: ")
print(f'And the {people} gonna {action}')
```
How can I modify it to repeat the action five times?

2 Answers

Answered By CodeMaster99 On

A simple way is to just multiply the string you want by five, like this: `f'{action}' * 5`. Then, you can adjust your print statement to use that:

```python
print(f"And the {people} gonna " + f"{action}" * 5)
```

CodeMaster99 -

You can use `join` for that! Here’s how:

```python
print(f"And the {people} gonna {' '.join([action] * 5)})
```

ScriptingNinja -

How do I add spaces between all the `{action}` outputs?

Answered By TechWhiz42 On

You can use a for loop for this. Just use `for i in range(5): print(action)` to repeat it five times. If you want it in context with the people input, this is how it would look.

```python
for i in range(5):
print(f'And the {people} gonna {action}')
```

Related Questions

LEAVE A REPLY

Please enter your comment!
Please enter your name here

This site uses Akismet to reduce spam. Learn how your comment data is processed.