I see a number of problems with your code.
Your for
loops will each return a value on the first iteration of the loop, so they aren't doing you any good as loops...there will never be a second iteration of either loop.
n
is undefined in your outer scope, so the line gen = odd(n)
will throw an exception stating that n
is undefined.
You have a problem with the definition of what you want your code to do. You want to generate a list of numbers and pass them to the odd
function, but then you say you want odd
to return True
or False
based on if "that number" is odd or even. But there isn't a single number. You're passing a list of numbers, and in general, some will be odd and some will be even.
Your interpretation of what n%2 != 0
means seems backwards.
Here's a version of your code that addresses all of these issues. I decided to replace your odd
function with a function named more_odd_than_even
. The function does as the name implies. It returns True
if more of the numbers in the list are odd than are even, and False
otherwise.
import random
def more_odd_than_even(the_list):
odd_count = 0
even_count = 0
for n in the_list:
if n % 2 == 0:
even_count += 1
else:
odd_count += 1
return odd_count > even_count
def producer(num):
my_list = []
for i in range(num):
n = random.randint(100, 2000)
my_list.append(n)
return my_list
def main():
ask = int(input('How many numbers do you want to generate? '))
return ask
num = main()
numbers = producer(num)
print(f"The generated list is:{numbers}")
gen = more_odd_than_even(numbers)
if gen:
print("More of the numbers are odd")
else:
print("More of the numbers are even (or there are an equal number of each)")
Here are a few sample runs of this code:
How many numbers do you want to generate? 6
The generated list is:[972, 1327, 1603, 1714, 491, 1007]
More of the numbers are odd
How many numbers do you want to generate? 7
The generated list is:[1540, 696, 904, 475, 1007, 1238, 148]
More of the numbers are even (or there are an equal number of each)