0

This program creates an array in main and prompts the user to enter 5 integers and stores them in a array in a different function. It then creates a function that accepts the array and returns a true if the array contains only odd numbers or false if it does not. Print a message indicating the results being either the array contains both odd and even numbers or the array contains only odd numbers.

I keep getting TypeError:Unsupported operand types for /: 'list' and 'int'.

def main():
    integer = 5
    intArray = [0] * integer
    for index in range(integer):
    intArray[index] = int(input("Enter integers:"))
    print(intArray)
    fibArray(integer)
    containsOnlyOdds(intArray)
    return intArray

def fibArray(num):
    fibArray = [0] * num
    for index in range(num):
        print(num)


def containsOnlyOdds(notEven):
    average = (notEven / 2) % 2
    for odd in range(average):
        if notEven %2 != 0:
            print('This array contains only odd numbers')
        else:
            print('This array contains odd and even numbers')

main()
1
  • If you are trying to learn python you should try to understand the error message. TypeError:Unsupported operand types for /: 'list' and 'int' What could this mean? Right you are trying to divide a list. This does not work. You need to set up a loop and iterate over the elements of that list. Commented Dec 3, 2013 at 7:26

1 Answer 1

3

Even without seeing your indentation, I can give you a simple way to check for all odds:

def check_list(lst):
    for num in lst:
        if num % 2 == 0:
            return False
    return True

The check_list() function takes a list argument and checks if each number in the list is divisible by 2. If a number is, clearly it is even and the function returns False. This is efficient because as soon as it finds an even number, it exits, rather than searching the entire list.

Now that you fixed your formatting:

It appears that you are trying to divide a list by a factor of 2 in your containsOnlyOdds function. It is not possible to divide a list, you need to set up a loop to loop through each index of the list, and then check if each number is divisible by 2 or not.

Sign up to request clarification or add additional context in comments.

8 Comments

For people more comfortable with Python than the OP, all_even = all(num % 2 == 0 for num in lst) does this as well, with short circuiting. But understanding the for loop version first is probably wise.
I was actually going to post a one-liner, but figured it made more sense here and for the OP to keep things as simple as possible
I had the indentation correct but when I posted it here it made change it. Can you offer any feedback on how to format better. Everything is appreciated. I will try your suggestion.
I figured your code was formatted correctly on your program, otherwise you would've had a different error, I meant fix formatting in your question
Your code helped me correct the error, however how would I attach the print statements to the return True or return False. When I run the program I prints (5) 5's in a line.
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.