简体   繁体   中英

how to make a python program using “while” to print at 0?

My assignment is to make a program that prompts the user to input numbers to be categorized as even or odd. It will then add them together and count the amount of inputs of evens or odds. So far, I have that down, but I can't figure out how to get my program to run through the outputs when 0 is typed in. Here's my code:

  number = int(input("Input an integer (0 terminates the program): "))
zero = 0
count_odd = 0
count_even = 0
odd_sum = 0
even_sum = 0
sum = float(count_odd) + float(count_even)

while number >= 0:
    if number%2 == 0:
        count_even+=1
        even_sum= even_sum + number
        number = int(input("Input an integer (0 terminates the program): "))
    elif number%2 != 0:
        count_odd+=1
        odd_sum = odd_sum + number
        number = int(input("Input an integer (0 terminates the program): "))
    elif number == zero:
        print("Sum of odds: " +odd_sum)
        print("Sum of evens: " + even_sum)
        print("Odd count: " +count_odd)
        print("Even count: " +count_even)
        print("Total positive int count:" +sum)
    else: 
        number = int(input("Input an integer (0 terminates the program): "))

I'm not even sure the ending is right at all. Especially not my attempt at creating a "zero."

When you input "0", this condition is true as well:

if number%2 == 0:

So your program counts the "0" as a valid input (an even number).

Try comparing with zero first, then the rest of the ifs.

Also, you should use a "break" to end the program when "0" is inputted.

Try this version:

    number = int(input("Input an integer (0 terminates the program): "))
    zero = 0
    count_odd = 0
    count_even = 0
    odd_sum = 0
    even_sum = 0
    sum = float(count_odd) + float(count_even)

    while number >= 0:
        if number == zero:
            print("Sum of odds: ", odd_sum)
            print("Sum of evens: ", even_sum)
            print("Odd count: ", count_odd)
            print("Even count: ", count_even)
            print("Total positive int count:", sum)
            break
        elif number%2 == 0:
            print "a"
            count_even+=1
            even_sum= even_sum + number
            number = int(input("Input an integer (0 terminates the program): "))
        elif number%2 != 0:
            count_odd+=1
            odd_sum = odd_sum + number
            number = int(input("Input an integer (0 terminates the program): "))
        else: 
            number = int(input("Input an integer (0 terminates the program): "))

Your current program does not work because 0 % 2 is also 0 , so it will go into the if block and it will never reach the elif number == zero: block.

You should move your elif to the top to become an if and move the if to elif , for your logic to work. and also, if you want to loop to break at 0 , you should add break statement in the number == zero block. Example -

while number >= 0:
    if number == 0:
        print("Sum of odds: ", odd_sum)
        print("Sum of evens: ", even_sum)
        print("Odd count: ", count_odd)
        print("Even count: ", count_even)
        print("Total positive int count:", even_sum + odd_sum)
        break
    elif number%2 == 0:
        count_even+=1
        even_sum= even_sum + number
        number = int(input("Input an integer (0 terminates the program): "))
    elif number%2 != 0:
        count_odd+=1
        odd_sum = odd_sum + number
        number = int(input("Input an integer (0 terminates the program): "))

Other issues that I noticed in your program -

  1. "Sum of odds: " +odd_sum would not work because you cannot add int and string like that, instead pass them as separate arguments to the print .

  2. You do not need the else: part, as it would never reach there.

I made some changes and addressed them with comments, the main problem was that you should have been checking for zero with the first conditional statement in your while loop.

# Setup your variables
# I removed the input line from here, since we only need to get input in the while loop
count_odd = 0
count_even = 0
odd_sum = 0
even_sum = 0

# Use a simple boolean flag to tell the while loop to keep going
keep_going = True
while keep_going == True:

    # Grab the number from the user
    number = int(input("Input an integer (0 terminates the program): "))

    # IMPORTANT - Check for zero first
    # If it is zero, set out flag to false, so we will exit the while loop
    if number == 0:
        keep_going = False

    elif number % 2 == 0:
        count_even += 1
        even_sum += number

    elif number % 2 != 0:
        count_odd += 1
        odd_sum += number

# Here we have exited the while loop
sum = float(count_odd) + float(count_even)    

# Print everything here

To fully make your script function you'll need this:

import sys # allows the script to end after 0 properly

number = int(input("Input an integer (0 terminates the program): "))
zero = 0
count_odd = 0
count_even = 0
odd_sum = 0
even_sum = 0
summation = float(count_odd) + float(count_even) # don't use sum because it's already taken by python, and you can also remove it, see below why

while number >= 0:
    if number%2 == 0 and number != 0:
        count_even+=1
        even_sum= even_sum + number
        number = int(input("Input an integer (0 terminates the program): "))
    elif number%2 != 0:
        count_odd+=1
        odd_sum = odd_sum + number
        number = int(input("Input an integer (0 terminates the program): "))
    elif number == 0: # pay attention to the concatenation of strings, not string and integer
        print("Sum of odds: " + str(odd_sum))
        print("Sum of evens: " + str(even_sum))
        print("Odd count: " + str(count_odd))
        print("Even count: " + str(count_even))
        summation = float(count_odd) + float(count_even)
        print("Total positive int count:" + str(summation)) # add everything up after it's been done NOT before
        sys.exit() # to stop your infinite loop
    else: 
        number = int(input("Input an integer (0 terminates the program): "))

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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM