Python 3.4.1 Print newline
I have a quick question that I've been trying to figure out for some time now. I am writing code that takes the entered ranges of numbers (high and low) and then uses the entered number to see if there are multiples of that range within the range. Then he adds up the sum of the odd and even numbers and adds how many there are. I have everything to calculate correctly, but my problem is that I cannot separate the line "90 75 60 45 30" from another line "3 even numbers up to 180 total". I'm sure this is something simple, but I can't figure it out. Can anyone point me in the right direction? well in advance for time and attention.
The code returned below:
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()
source to share
A minimal example of a problem:
>>> def test1():
for _ in range(3):
print("foo", end=" ")
print("bar")
>>> test1()
foo foo foo bar # still using end=" " from inside the loop
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 blank print
can be located anywhere between the end of the loop for
in which you print
separate the numbers and print(even_count, ...
, for example:
...
odd_sum += number
print()
print(even_count, 'even numbers total to', even_sum)
source to share