Summary
Every once in a while I like to browse through a few Facebook groups and assist people that need help with their code. Generally doesn’t matter if I’m able to assist them right away. I’ll go through the comments and assist them if the code still isn’t working.
The description from the post of the user asking the question can be found below. Just to provide context.
Hello guys how can I finish this code by using for loop and iterating each number weather it’s even or odd. Note I don’t want do this code by using map.
The code they provide can be found in the next section. I want to be clear that these posts aren’t intended to belittle anyone. These are just intended to iron out what I’ve learned and see who may benefit from this in the future.
Facebook Users Code
Below is their code. There are some things going on with it that should probably be pointed out. Just some tips.
- When you iterate through a list or anything. You don’t want to use the list we’re iterating through as a value for the return. We want to utilize the item we’re currently on. Not returning the current iteration of the list will provide unexplained results because it’s not returning the result of the operation. Rather it’s returning the actual list or number the operation it’s being performed on.
- This function should be designed to perform a specific task. In this case they’re attempting to obtain the result of whether a number is even or odd. But, they’re also acting under the assumption that a list of numbers will be passed into the function every time. Which is something that we should probably never assume. But, there is a potential that it could happen or may be needed.
Some more explanation can be provided at the output portion of these notes.
def even_or_odd(a):
for num in a:
if a % 2 == 0:
return 'a is {} even'.format(a)
else:
return 'a is {} odd'.format(a)
print(even_or_odd(list[1,2,3,4,5]))
This is the output from their code when you attempt to run it. Note, that I don’t have any idea of which Python version this individual is using.
First of all we get a TypeError. Considering that they aren’t requesting user Input we can’t dynamically put this into a list. Should probably be added to a variable or be generated from user input. There are a lot of things to explain as to where the code has gone wrong. This person is learning. The simplest answer to this error is that it doesn’t follow the PEP 585 standard.
Traceback (most recent call last):
File "/home/example/code/python/testing/fbh1/fbh1.py", line 8, in <module>
print(even_or_odd(list[1,2,3,4,5]))
File "/home/example/code/python/testing/fbh1/fbh1.py", line 2, in even_or_odd
for num in a:
TypeError: 'types.GenericAlias' object is not iterable
It may be best to rewrite the code above. Instead of attempting to fix it. Remove a few things like the loop from the function and perform the iteration outside of it. There was no request for user input. So, I won’t assume that it’s required.
Fixed Code
The fixed code is below. I will explain it a little.
- Created a list variable named numbersList and added numbers 1 through 6 to it.
- Created a function that you pass the num parameter into. The num parameter is then used in the if else statements to determine if it’s odd or even. Checks to see if there is a remainder of zero when it’s divided by two. If it is, it will output as even. If it’s not zero. It will output as odd.
- Iterate through the numbersList variable and print the output with the print function returned from the EvenOrOdd() function.
"""
file: even_or_odd.py
author: Timothy (n3s0)
date: 06/15/2022
"""
# List of numbers to check.
numbersList = [1,2,3,4,5,6]
def EvenOrOdd(num):
"""
Check and return if the number is even or odd.
"""
if num % 2 == 0:
return "Number {} is even!".format(num)
else:
return "Number {} is odd!".format(num)
# Iterate through all the numbers in the list
# Call EvenOrOdd() on each iteration
for n in numbersList:
print(EvenOrOdd(n))
Should be able to run the code using the following command after creating the file. Make sure Python is installed on the workstation you’re on first.
python even_or_odd.py
Below is the output from the code. As we can see, it provides the desired output for this operation. The script iterates through the list of numbers and outputs if their an even or odd number.
Number 1 is odd!
Number 2 is even!
Number 3 is odd!
Number 4 is even!
Number 5 is odd!
Number 6 is even!
Conclusion
Wasn’t able to provide my full answer to them in the end. I don’t believe the admins of the group approved my initial comment so I could get that conversation started. But, there you go. There is the code and it works.
Of course if there are any questions. Anyone is welcome to send me an email.