Home > Back-end >  Python program to read whole numbers and stop when 0 entered, then prints total positive numbers ent
Python program to read whole numbers and stop when 0 entered, then prints total positive numbers ent

Time:04-04

I am trying to write a program where a user enters a bunch of numbers and when 0 is entered the program ends and prints how many positive numbers were entered. I am fairly close to solving this but can't get the positive sum to print. Could you please explain where I have gone wrong?

Thanks in advance.

Please see attached my code.

userInput = None
oddNum = 0
evenNum = 0

while(userInput != 0):
    userInput = int(input("Enter a number: "))
    if(userInput > 0):
        if(userInput % 2 == 0):
            evenNum  = 1
        elif(userInput % 2 != 0):
            oddNum  = 1

print("positive numbers were entered".format(evenNum, userInput))

CodePudding user response:

You are missing some of the required syntax for the string.format() command.

For each variable you want injected into the string to be printed, you must include a pair of curly braces {}. Otherwise, the arguments you pass to string.format() are just ignored.

userInput = None
oddNum = 0
evenNum = 0

while(userInput != 0):
    userInput = int(input("Enter a number: "))
    if(userInput > 0):
        if(userInput % 2 == 0):
            evenNum  = 1
        elif(userInput % 2 != 0):
            oddNum  = 1

print("positive numbers were entered; {} even numbers, and {} odd numbers".format(evenNum, userInput))

  • Related