Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Python program to read whole numbers and stop when 0 entered, then prints total positive numbers entered

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.

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

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))

>Solution :

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))

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading