0

I have to have three defined functions. The first asks the user for an input and it is returned to the next which creates random numbers in the range specified in the first function. That is then returned to the third function which checks all the numbers and returns true if they are even and false if they are odd. I have this so far, but I get the error "TypeError: 'int' object not iterable." I'm not sure what I'm doing wrong.

import random

def odd(n):
    my_list = []
    my_list.append(n)
    for i in len(my_list):
        if n%2 != 0:
            return False
        else:
            return True
    
def producer(num):
    for i in range(num):
        n = random.randint(100,2000)
        return n

def main():
    ask = int(input('How many numbers do you want to generate? '))
    return ask

num = main()
numbers = producer(num)
gen = odd(n)

print(gen)
  • The problem is not well-defined. You are checking a list of numbers. Are you expecting `True` when _all_ the numbers are even and `False` when _all_ the numbers are odd? If so, what do you expect when some are even and the others are odd? Or, do you expect a list of `True` and `False`s, corresponding to each number in the input list? – j1-lee Mar 06 '22 at 03:49
  • I'm trying to check each individual number. I assumed this was doing that. If it's not, how do I make it check each number? – RedParagon Mar 06 '22 at 03:50
  • Welcome to Stack Overflow. I see a number of problems with your code in addition to what has already been pointed out. With both of your `for` loops, your code is going to return a value on the first iteration through the loop, so the loops aren't really doing you any good. Also, I don't think this code will even run. You aren't defining `n` in the outer scope of your code, so `gen = odd(n)` will throw an exception telling you that `n` is undefined. And then there's the question about what you want the code to do. If your list has 4 even numbers and 3 odd numbers, what should `odd` return? – CryptoFool Mar 06 '22 at 04:17
  • Step by step, this is what I'm trying to do: 1. Define a function that asks for an input and returns that input. 2. Define a function that takes the input from the first function and creates numbers in the range selected. So if I put 6 for the first function, the second will take that and create 6 random numbers. 3. I want those numbers sent to a third defined function that checks each number and tells the user if that number is odd or even by returning true if it is even and false if it is odd. – RedParagon Mar 06 '22 at 04:24
  • But you say "NUMBERS (plural) sent to a third defined function" but then say "if THAT NUMBER is odd or even". Which NUMBER are you talking about? If you pass a list of numbers to the third function, and some are odd and some are even, what should the function return? – CryptoFool Mar 06 '22 at 04:27
  • Oh, I think I see. Then I need to split the list back up into individual numbers that are then checked. – RedParagon Mar 06 '22 at 04:39

3 Answers3

0

the error is because of the for i in len(my_list). len(my_list) will return an int.

you want "for num in my_list" instead. then inside the function, "num%2" instead of "n%2".

Syd
  • 1
  • 2
0

I see a number of problems with your code.

  1. 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.

  2. n is undefined in your outer scope, so the line gen = odd(n) will throw an exception stating that n is undefined.

  3. 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.

  4. 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)
CryptoFool
  • 21,719
  • 5
  • 26
  • 44
0

Is this what you are trying to achieve:

import random


def get_user_input():
    # The first asks the user for an input and it is returned to the next
    user_input = int(input('How many numbers do you want to generate? '))
    return user_input


def producer(n):
    # which creates random numbers in the range specified in the first function.
    random_list = [random.randint(100, 2000) for i in range(n)]
    return random_list


def odd(lst):
    # That is then returned to the third function which checks all the numbers and
    for n in lst:
        if n % 2 == 0:
            # returns true if they are even
            print(True)
        else:
            # and false if they are odd
            print(False)



if __name__ == '__main__':   
    user_input = get_user_input()    
    list_range = producer(user_input)    
    odd(list_range)  

Seraph
  • 374
  • 3
  • 14
  • Yes! Can you explain the if statement? I don't understand how it automatically calls the functions. I thought you needed to call a function before anything works. And what are you saying in that statement....? What I mean is what is __name__ == '__main__'? – RedParagon Mar 06 '22 at 04:47
  • Here is a good explanation of what if **__name__ == "__main__:** does: [answer](https://stackoverflow.com/questions/419163/what-does-if-name-main-do). Basically, If the program is run (instead of imported), run the program. – Seraph Mar 06 '22 at 05:21