Python 3.4.1 Print new line
Solution 1
print('\n', even_count, ' even numbers total to ', even_sum, sep='')
should do it. Just manually put in a new line somewhere
Solution 2
A minimal example of the problem:
>>> def test1():
for _ in range(3):
print("foo", end=" ")
print("bar")
>>> test1()
foo foo foo bar # still using end=" " from inside the loop
A minimal example of one solution:
>>> def test2():
for _ in range(3):
print("foo", end=" ")
print() # empty print to get the default end="\n" back
print("bar")
>>> test2()
foo foo foo
bar
This empty print
can sit anywhere between the end of the for
loop in which you print
the individual numbers and print(even_count, ...
, for example:
...
odd_sum += number
print()
print(even_count, 'even numbers total to', even_sum)
WillyJoe
Updated on June 09, 2021Comments
-
WillyJoe almost 3 years
I have quick question that I have been trying to figure out for some time now. I am writing a code that takes inputted number ranges(a high, and a low) and then uses an inputted number to find out if there are multiples of that number within the range. It will then add total the sum of odd and even numbers and add how many there are. I've got everything to calculate correctly but my problem is I can't separate the line "90 75 60 45 30" from the other line "3 even numbers total to 180". I'm sure it's something simple but I can't figure it out. Would someone be able to point me in the right direction? thanks in advance for time and consideration.
The below code returns:
Number of high range?: 100 Number of low range?: 20 Multiple to find?: 15 90 75 60 45 30 3 even numbers total to 180 2 odd numbers total to 120
Code:
def main(): x = int(input('Number of high range?: ')) y = int(input('Number of low range?: ')) z = int(input('Multiple to find?: ')) show_multiples(x,y,z) def show_multiples(x,y,z): for a in range(x,y,-1): if a % z == 0: print (a,end=' ') even_count = 0 even_sum = 0 odd_count = 0 odd_sum = 0 for num in range(x,y,-1): if num % z == 0 and num % 2 == 0: even_count += 1 even_sum += num for number in range(x,y,-1): if number % z == 0 and number % 2 == 1: odd_count += 1 odd_sum += number print(even_count,'even numbers total to',even_sum) print(odd_count,'odd numbers total to',odd_sum) main()
-
Padraic Cunningham over 9 yearsthis does not put the output on separate lines
-
jonrsharpe over 9 years@PadraicCunningham don't follow, sorry; this approach will split up
90 75 60 45 30 3 even numbers total to 180
onto two separate lines. -
Padraic Cunningham over 9 years@jonsharpe, it puts it all on one line, I mean using
print(even_count,'even numbers total to',even_sum,end=" ")
unless I am misunderstanding you -
jonrsharpe over 9 years@PadraicCunningham ah, perhaps it isn't clear - the
print()
sits between the end of thefor
loop andprint(even_count, ...
. -
Padraic Cunningham over 9 years@johnsharpe, so no need for
end = " "
, sorry I did not follow.