2

How do I stop aList from flowing into sumIs? I've tried a couple of line breaks after aList's to close it. A break in aList stops the for loop and still flows into the sumIs.

code:

aList = ['spam', 'eggs', 'ham']
for x in aList:
    print(x, end = ' ')

sumIs = 0
for number in [1, 2, 3, 4]:
    sumIs = sumIs + number
print(sumIs)

prod = 1
for item in [1, 2, 3, 4]:
    prod *= item
print(prod)

S = 'lumberjack'
T = ("and", "I'm", "okay")
for char in S:
    print(char, end=' ')

print as of now is:

spam eggs ham 10
24
l u m b e r j a c k 

print should be:

spam eggs ham 
10
24
l u m b e r j a c k
1
  • 1
    Please do not "flatten" for loops into one-liners. Please use 4 spaces for your indentation. Commented Jan 6, 2012 at 19:24

1 Answer 1

4

Since your print()-call in the loop doesn't print any line returns, you have to print the line return after the loop.

Simply add a print() after the loop.

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

3 Comments

end = ' ' is still pulling 10 into the same line.
ha, you literally meant add an empty print() after the for loop body. Thanks!
@milkIt: Exactly. end=' ' doesn't pull anything in, it's the other way around. Without end=' ' the end will be a newline. So it's not a pull, it's an absence of push.

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.