Python Forum
newbie in python. odd ,even & Zero counter. output not crrect
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
newbie in python. odd ,even & Zero counter. output not crrect
#1


take input from user , size of list , then numbers and then count zer0s,even and odds. Code is working fine for odd number and even. but when user enter "0" this counted in zeros and in EVENS as well. 0 is not an even number . Please help

n = int(input ("enter list size "))

zero = 0
even = 0
odd = 0
i = 0
for i in range (i,n):
number = int (input ("enter number "))
result = number % 2
if (number==0):
zero = zero + 1
if (result == 0):
even = even +1



if (result == 1 or result == -1 ):

odd= odd+1



print ("zero =" + str (zero) )
print ("even =" + str ( even) )
print ("odd =" + str (odd) )
Reply
#2
1. Use code tags !

2. use if elif else
99 percent of computer problems exists between chair and keyboard.
Reply
#3
As Windspar said, you need to use elif to make distinct paths:

    if (number==0):
        zero = zero + 1
    elif (result == 0):
        even = even +1
    elif (result == 1 or result == -1 ):
        odd= odd+1
I am trying to help you, really, even if it doesn't always seem that way
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Newbie to CS and Python, 2 questions johneven 2 2,191 Feb-01-2019, 03:00 AM
Last Post: johneven
  Python Counter - Convert to a Class jill1978 1 2,072 Oct-16-2018, 10:52 AM
Last Post: Larz60+

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020