0

This program should create an array in main and prompts the user to enter 5 integers and store them in the array in a function called fibArray. It then creates a function named containOnlyOdd that excepts 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 such as ex: [1,2,3,4,5] "The array contains both odd and even numbers." or [1,3,5,7,9] "The array contains only odd numbers.

The error is get is in line 10 in fibArray which is fibArray = [0] * num. TypeError: Can't multiply sequence by non-int of type list.

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

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

def containsOnlyOdds(lst):
    for num in lst:
    if num % 2 == 0
        #print("This list is all odd")
         return false`
    #print("This list has even numbers")            
    return True

main()

What am I doing wrong? Please help!!

4
  • 4
    please please please rephrase the title. "I tried that"?... you tried what? Commented Dec 3, 2013 at 21:45
  • You're passing a list where it looks like your expecting an int. Commented Dec 3, 2013 at 21:59
  • @ shx2 I changed the title :). @ valeHalfHeart any suggestions or help would be great. I am a beginner!! What should it look like? Commented Dec 3, 2013 at 22:35
  • In addition to the problems you are having with the main error message, you will need to properly indent you "if" statement in the function containsOnlyOdds. Commented Dec 3, 2013 at 22:46

1 Answer 1

1

It is exactly, what the error message states: you hand an array to fibArray:

fibArray(intArray)

which in turn tries to multiply it with [0]:

fibArray = [0] * num

You may only call fibArray and pass a number, but not an array.

edit/add:

The numbers are already stored in the array intArray. No need for the function fibArray. Of course you could alternatively pass the numbers to a function which then again stores the values. Since you are just beginning python, i recommend leaving it with the value-checking-function for now. Once the program works, you can try to encapsulate part of the code in another function.

There is also some syntax errors in the definition of def containsOnlyOdds(lst): You should fix the indentation of the loop and if-statement. Also, you are missing a colon after the statement, e.g.

for anElement in aList:
    if (statement):
        #do something, e.g. return False
    #do something else, e.g. return True
Sign up to request clarification or add additional context in comments.

3 Comments

Any suggestions on changes I should apply to make it work. I am greatful for any help. I am a beginner at all this.
I added the variable num = 5 in the fibArray function and the code works, but after I run it prints 5 5's not the print statement. So i can print from my last function.
Of course it will print 5 5s.. you have a statement that says: for index in range(num): print(num).. Like I suggested earlier, please check on the definition and usage of loops and come back once you have a new question.

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.