-4

my code is about :count how many values are even , positive , negative and it's not working : thanks for your help

e = [ ] 

for c in range(5):

    c=float(input())
    if c<0:
        e.append(c)
        print("{} valor(es) negativo(s)".format(len(e)))
    if c>0:
        e.append(c)
        print("{} valor(es) positivo(s)".format(len(e)))
    if c%2!=0:
        e.append(c)
        print("{} valor(es) par(es)".format(len(e)))
    if c%2==0:
        e.append(c)
        print("{} valor(es) impar(es)".format(len(e)))

i want the o/p like this :

3 valor(es) par(es)

2 valor(es) impar(es)

1 valor(es) positivo(s)

3 valor(es) negativo(s)

when i enter the five ( int )nos and exit when input==4

Maha Taha
  • 7
  • 1
  • 7
  • 4
    Ok, so... Are you getting an error or something? What's your question? – Kevin Aug 29 '16 at 20:16
  • 2
    Please revise your question and be more specific about what your problem is? What is currently not working in your code? What is it doing that you think it should not be doing? What are the inputs and expected outputs? Please make sure you meet the requirements of a [mcve] – idjaw Aug 29 '16 at 20:16
  • Yes. Please specify a question. – Christian Dean Aug 29 '16 at 20:16
  • And your problem is? You're not even using `elif` in this code. – ShadowRanger Aug 29 '16 at 20:17
  • And why do you add each value to `e` one or two times? – Matthias Aug 29 '16 at 20:17
  • Are you asking how to use `elif`? – Christian Dean Aug 29 '16 at 20:17
  • @MahaTaha: What is it doing? What do you want it to do? "Not working" could mean anything from "Getting an exception" to "I'm missing a comma in the output". – ShadowRanger Aug 29 '16 at 20:18
  • So why not just say its not working in your question, rather then beating around the bush. – Christian Dean Aug 29 '16 at 20:18
  • Also note, `n%2 != 0` is not really testing what you might think; I'm assuming you're trying to distinguish even and odd numbers, but even and odd don't make sense for arbitrary `float` values; is `12.5` even or odd? `12.5 % 2` produces `0.5`, so it will count as odd (as will every value that isn't an even integer value, and depending on floating point precision issues, possibly some even integer values too). Did you mean to convert to `int`? – ShadowRanger Aug 29 '16 at 20:22
  • @ShadowRanger ... i edited my Q – Maha Taha Aug 29 '16 at 20:41

1 Answers1

2

First you are using the same list for all value types. Count will be correct only the first time! see below for odd & even numbers. Same error above for positive / negative.

Second this is functionnaly wrong:

if c%2!=0:
    e.append(c)
    print("{} valor(es) par(es)".format(len(e)))
if c%2==0:
    e.append(c)
    print("{} valor(es) impar(es)".format(len(e)))

Define:

pares = []
impares = []

in your loop, write this

if c%2==0:
    pares.append(c)
    print("{} valor(es) par(es)".format(len(pares)))
else:
    impares.append(c)
    print("{} valor(es) impar(es)".format(len(impares)))

proof of concept and trying to be as pythonic as possible (I have made the example non-interactive, and using only integers. Like ShadowRanger noted, modulo on floats won't work very well with this code)

# define 4 lists
the_list = [list() for i in range(4)]
negative_values,positive_values,odd_values,even_values = the_list

z=[1,2,-5,-7,0,3,-4]
for c in z: #range(5):

    #c=float(input())
    if c<0:
        negative_values.append(c)
    elif c>0:
        positive_values.append(c)
    if c%2==0:
        even_values.append(c)
    else:
        odd_values.append(c)

print("{} negative values, {} positive values, {} odd values, {} even values".format(*tuple(len(x) for x in the_list)))
Jean-François Fabre
  • 137,073
  • 23
  • 153
  • 219