I am new to python, and I just learned loops. I am trying to write a program that reads six integers and then finds the sum of the even and odd integers, using a while loop. I am not sure how to use the while loop in this situation. This is what I started with:
print('Please enter 6 integers:')
n_1=int(input('>',))
n_2=int(input('>',))
n_3=int(input('>',))
n_4=int(input('>',))
n_5=int(input('>',))
n_6=int(input('>',))
Even_sum = 0
Odd_sum = 0
while
Instead of writing a line for each input you could use the while loop to keep getting inputs for n times (6 in your example). Also, it's better to initialise both sums at the very beginning and add to each of them every even or odd input that is given. You also need to define a counter or a boolean statement that will stop the loop.
sum_even = 0
sum_odd = 0
i=0
while i < 6:
num = int(input())
# check if input is even
if (num % 2) == 0:
sum_even+=num
else:
sum_odd+=num
i+=1
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.