0

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!

2 Answers 2

3

Use a loop to repeat the command. Try:

print("Hello")
print("World")
for x in range(5):
    print("Foo")
Sign up to request clarification or add additional context in comments.

1 Comment

should be 5, not 4: for x in range(5):
2

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))

1 Comment

And further in learning to program; find the available options and then use one that makes the code easily understandable and also easy to modify; e.g. You yourself must understand what is intended to happen after not looking at it for a year or so.

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.