vendredi 2 mars 2018

What Is The Best Practice When it Comes To Determining and Counting Odd and Even Numbers in python

For a recent assessment I completed in my studies, I have a piece of code that asks the user for four integer numbers. Then the program must count and display the number of odd and even numbers in the entries. The code is as follows:

# xx assignment 02 question 2
a = int(input("Enter an integer number: "))
b = int(input("Enter an integer number: "))
c = int(input("Enter an integer number: "))
d = int(input("Enter an integer number: "))
e = 0
o = 0

if a % 2 == 0:
    e = e + 1
else:
    o = o + 1
if b % 2 == 0:
    e = e + 1
else:
    o = o + 1
if c % 2 == 0:
    e = e + 1
else:
    o = o + 1
if d % 2 == 0:
    e = e + 1
else:
    o = o + 1

print("Number of even numbers entered: ", e)
print("Number of odd numbers entered: ", o)

The code I have works perfectly, I am not looking for a way to fix the code since it works. I would like to know if the reason for this excercise being marked incorrect.

The reason given for the mark is "Rather use a For loop than a series of if-else statements!!!!"

Now I could understand that if this is a crucial part of programming that it were to get marked wrong even though the question's goal was completed successfully.

Since I have just recently started learning python, am I missing something or is there a standard to follow that requires you to use a for loop when you want to compare a user's input to a formula, and then performing something depending on the output?

Note: I'm not posting this to prove a point to somebody who is just following what they have done for the past few years, as I am learning and I might be wrong and I would not know why.

Aucun commentaire:

Enregistrer un commentaire