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()
print('\n', even_count, ' even numbers total to ', even_sum, sep='')
should do it. Just manually put in a new line somewhere
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)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With