-1

I'm taking a class in Python and our prof wants us to write a program that prompts the user to enter an integer repeatedly until they enter 0. Then, have the program ignore all negative numbers, if any, and display the number of even integers, the number of odd integers, the sum of the even integers, the sum of the odd numbers, and the number of positive integers.

I've been trying and trying to do this program in small parts. However, I always end up getting stuck. I've started over about 5 times now and I would really appreciate if someone were to point me in the right direction.

So far, I have this:

 num_str = input("Input an integer (0 terminates):")
 num_int=int(num_str)
 even_count=0
 odd_count=0
 even_sum=0
 odd_sum=0 


while num_int !=0:
   num_str = input("Input an integer (0 terminates):")
   num_int=int(num_str)
   for num_int in num_str: 
       if num_int%2 == 0: 
           even_count += 1
       else: 
           odd_count +=1


print("")
print("Sum of odds:", odd_sum)
print("Sum of evens:", even_sum)
print("Even count:", even_count)
print("Odd count:", odd_count)
print("Total positive int count:")

I know it's not much and I'm missing a whole lot, but I just wrote what I know needs to be included so far. I keep getting stuck because the program keeps giving me errors. Any sort of help is very appreciated because I have really no idea where to start!

Pang
  • 9,564
  • 146
  • 81
  • 122
Nora
  • 15
  • 1
  • 2
  • 7

6 Answers6

-1

to ignore negative numbers, you could have them put it in agian, with an if loop like this if (num_str>0): num_str = input("That was not an even number, input an integer (0 terminates)") Then to add them you would have to add the integer version of num_str to it like this odd_sum += int(num_str) here's some code for you to try

num_str = input("Input an integer (0 terminates):")
num_int=int(num_str)
even_count=0
odd_count=0
even_sum=0
odd_sum=0 
total = even_count + odd_count

while num_int !=0:
    num_str = input("Input an integer (0 terminates):")
    num_int=int(num_str)
    if num_int < 0:
        num_str = input("Input an integer greater than 0.")
    for num_int in num_str:
        num_int = int(num_str)

        if num_int % 2 == 0 and not num_int == 3 and not num_int == 0: 
            even_count += 1
            even_sum = even_sum + num_int
        elif not num_int == 0: 
            odd_count +=1
            odd_sum = odd_sum + num_int
    total = even_count + odd_count

print("")
print("Sum of odds:", odd_sum)
print("Sum of evens:", even_sum)
print("Even count:", even_count)
print("Odd count:", odd_count)
print("Total positive int count:", total)
Travis
  • 163
  • 1
  • 3
  • 17
-1

Your code has a few problems, but they're small:

1) You're asking for numbers before your main loop, so the first integer entered wouldn't be summed (lines 1 and 2)

2) It doesn't make sense for you to have a for loop like the one in your main loop. What you were doing is trying to check for each character in the string. Just not what you'd want.

3) To ignore negative numbers, just check if they're less than 0 and continue (break the loop) if they are.

4) You were using indentation with 3 spaces. It's probably your text editor's fault, so try to configure it to use 4 spaces instead, which is the standard in Python.

5) Convention says there should be a space around operators.

6) Positive integer count is just another simple counter.

All that revised, this is what your code should look like:

num_int = None
even_count = 0
odd_count = 0
even_sum = 0
odd_sum = 0


while num_int != 0:
    num_str = input("Input an integer (0 terminates):")
    num_int = int(num_str)

    if num_int < 0:
        continue  # Continue the loop and go back to asking input

    # If the loop reaches this point we know it's a positive number, so just add one
    positive_count += 1

    if num_int % 2 == 0:
        even_count += 1
        even_sum += num_int
    else:
        odd_count +=1
        odd_sum += num_int


print("")
print("Sum of odds:", odd_sum)
print("Sum of evens:", even_sum)
print("Even count:", even_count)
print("Odd count:", odd_count)
print("Total positive int count:", positive_count)
-1

try this

userInput = None
oddSum = 0
oddCount = 0
evenSum = 0
evenCount = 0

while(userInput != 0):
    userInput = int(input("Enter a number: "))
    if(userInput > 0):
        if(userInput % 2 == 0):
            evenSum += userInput
            evenCount += 1
        elif(userInput % 2 != 0):
            oddSum += userInput
            oddCount += 1

print("even numbers: {} sum: {}".format(evenCount, evenSum))
print("odd numbers: {} sum: {}".format(oddCount, oddSum))
rsanath
  • 1,154
  • 2
  • 15
  • 24
  • There's no reason to `global` the variables, as that can end up causing namespace collisions in larger programs, as well as too many variables to manage individually. It'd make more sense to just pass the values in as *args or **kwargs, then manipulate and return if needed. – Casey May 23 '17 at 02:01
  • Indeed but this is a small program as he said and how do you suggest an alternative by passing arguments? – rsanath May 23 '17 at 03:41
  • This is a poor, unnecessary and not really correct use of recursion. The program also doesn't terminate when the user enters `0` after the first number. – Steven Summers May 23 '17 at 03:45
  • thanks for making me aware of that redundancy and mistake @StevenSummers – rsanath May 23 '17 at 04:33
-1
val = []
inpt = None
evensm, oddsm = 0, 0

while inpt != 0:
    inpt = int(input("Enter a number: "))
    val.append(inpt)

for i in val:
    if i % 2 == 0:
        evensm += i
    else:
        oddsm += i

print("Sum of even integers is", evensm)
print("Sum of odd integers is", oddsm)

Or if you don't prefer using lists:

oddsm = 0
evensm = 0

while 1:
    inpt = int(input("Enter a number: "))
    if inpt == 0:
        break
    elif inpt % 2 == 0:
        evensm += inpt
    else:
        oddsm += inpt

print("Sum of odd integers is", oddsm)
print("Sum of even integers is", evensm)
SkullBlazer
  • 11
  • 1
  • 5
  • 3
    Thanks for posting! Your answer would be more helpful if you explained what distinguishes it from the other posted answers. Why would someone go with your approach over the other 4 answers? – divibisan Mar 15 '19 at 16:42
  • I'm using lists as a way to store the values entered and then arranging them. I guess lists is the reason. Also, it's quite shorter and simpler to understand. – SkullBlazer Mar 16 '19 at 10:04
-1
Write a Python program to take input of positive numbers, with an appropriate prompt, from the user until the user enters a zero. Find total number of odd & even numbers entered and sum of odd and even numbers. Display total count of odd & even numbers and sum of odd & even numbers with appropriate titles.
    sumOdd =0
sumEven = 0
cntOdd =0
cntEven = 0
while True :
    no = int(input("enter a number (0 for exit)"))
    if no < 0 :
        print("enter positive no......")
    elif no == 0 :
        break
    elif no % 2 == 0 :
        cntEven = cntEven+1
        sumEven  = sumEven + no
    else :
        cntOdd = cntOdd+1
        sumOdd  = sumOdd + no
print ("count odd == ",cntOdd)
print ("sum odd ==  ",sumOdd)
print ("count even == ",cntEven)
print ("sum even ==  ",sumEven) 
-1
Write a Python program to take input of a positive number, say N, with an appropriate prompt, from the user. The user should be prompted again to enter the number until the user enters a positive number. Find the sum of first N odd numbers and first N even numbers. Display both the sums with appropriate titles.  
n = int(input("enter n  no.... : "))
sumOdd =0
sumEven = 0
for i in range (n) : 
    no = int(input("enter a positive number : "))
    if no > 0 :
        if no % 2 == 0 :
            sumEven = sumEven + no
        else :
            sumOdd = sumOdd + no
    else :
        print("exit.....")
        break
print ("sum odd ==  ",sumOdd)
print ("sum even ==  ",sumEven)