简体   繁体   中英

Python Odd Number counting program continues to give me an error

I am trying to make a program that asks the user to input numbers, using a while loop and then eventually return how many are odd.

Here is my code (I am terrible at formatting so feel free to edit!):

count = 0

num = int(input("Enter your number: "))
print ("Here is your number: " + str(num))

while num != -1:

    if num % 2 == 1:
        count +=1
    input num = int(input("Enter your number: "))

    print(num)

print(count)

NOTE : -1 should not be counted as a value! (this was an instruction included)

A simple rule to figure out how many odd numbers are between zero and a given number is to first check if its an odd number and if it is, then divide it by two and round it UP and that is your odd numbers count. If it is even, simply divide it by two and that is your odd number count.
Even example: given number 12. Dividing it by 2 is 6. (1,3,5,7,9,11)
Odd example: given number 15. Dividing it by 2 is 7.5 rounding it up is 8 (1,3,5,7,9,11,13,15)

Updated to match OP requirements

The code when it is launched will ask the user to input a list of numbers separated by commas and will return for each number in the list if it is even or odd, also a running count of odd numbers will be shown.

Python code:

def odd_counter(int_input):
    counter = 0
    for n in int_input:
        if n % 2 == 1:
            counter += 1
            print("{} number is ODD. Total count of odd numbers is: {}".format(n,counter))
        else:
            print("{} number is EVEN. Total count of odd number is: {}".format(n,counter))

numb_list = input("Please enter numbers seperated by comma: ")

numb_list = [int(numb) for numb in numb_list.split(',')]

odd_counter(numb_list)

Try something like this.

count = 0
num = 0
while num != -1:
    inp = input("Enter your number: ")
    num = int(inp)
    print ("Here is your number: " + inp)
    if num % 2 == 1:
        count += 1

print (count)

This will keep asking the user for a number until they enter -1.

You need to ensure that the while loop ends at some point. In your example, the user would have to enter -1 to get out of it and see count printed.

Try this code below (it is Python 2 because your initial code appeared to be this version).

count = 0
entries = 0
max_entries = 5 # Or however many you want

while entries < max_entries:
    # Check number is odd and not -1 (could also check number is >0 if need to exclude all negative numbers)
    num = int(input("Enter your number: "))
    print "Here is your number: " + str(num)
    entries += 1
    if num % 2 == 1 and num != -1:
        count += 1

print str(count) + " numbers are odd"

I should add that you haven't mentioned what problem you're having, so I'm assuming it's that the loop never ended. Also, on line 10, input num = is a syntax error. The input at the start is unnecessary.

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