ecanus yofiel
ecanus yofiel

Reputation: 49

How to repeat python lines for running commands?

I'm beginner on python, and i tried to repeat my python command from line 3 until line 7 on this code

print("Hello")
print("World")
print("Foo")
print("Foo")
print("Foo")
print("Foo")
print("Foo")

but i don't want to repeat that foo for 4 times, i want to make it shorter like this

print("Hello")
print("World")
print("Foo")
print ln 3 * 4 times again

but it did't work(yeah i know it supposed to be not worked)

to someone who knows or can give the solution please help me, thanks!

Upvotes: 0

Views: 69

Answers (2)

Reddy Lutonadio
Reddy Lutonadio

Reputation: 302

Use a loop to repeat the command. Try:

print("Hello")
print("World")
for x in range(5):
    print("Foo")

Upvotes: 3

bitinerant
bitinerant

Reputation: 1561

The answer from Reddy Lutonadio is correct, but the code below is a little shorter and teaches some concepts about Python Lists:

for s in ["Hello", "World"] + ["Foo"]*5:
    print(s)

Or, even shorter:

print("\n".join(["Hello", "World"] + ["Foo"]*5))

Upvotes: 2

Related Questions