-4

this is the python code i am working on 2 hours of sleep please help me get this to work the question for the code was to write a program to multiply all the elements of the list at even indexes.

def EvenProduct(arr, n):
    even = 1
    for i in range (0,n):
        if (i % 2 == 0):
            even *= arr[i]
    print("Even Index Product : " , even)

# Driver Code

arr = int(input("Enter the size of the list "))
print("\n")
num_list = list(int(num) for num in input("Enter the list items separated by space ").strip().split())[:arr]

print("User list: ", num_list)
n = len(arr)

EvenProduct(arr, n)

and i got this error

Traceback (most recent call last):
  File "<string>", line 26, in <module>
TypeError: object of type 'int' has no len()
Random Davis
  • 6,662
  • 4
  • 14
  • 24
  • Welcome to Stack Overflow. Please read [ask] and note well that this is **not a discussion forum**. We are not interested in the difficulty you are having with the code; we are interested in a clear **question** that emerges from your own best effort to [locate](https://ericlippert.com/2014/03/05/how-to-debug-small-programs/), [understand](https://meta.stackoverflow.com/questions/261592) and demonstrate the problem (in a [mre]). Please also read the [formatting help](https://stackoverflow.com/help/formatting). – Karl Knechtel Sep 16 '22 at 16:18
  • Reference: [I'm getting a TypeError. How do I fix it?](https://stackoverflow.com/questions/73631401) – Karl Knechtel Sep 16 '22 at 16:19
  • You have one line, `num_list = list(...`, doing a ton of things. It's better to break such lines up into multiple lines, so that you can debug more easily. – Random Davis Sep 16 '22 at 16:20
  • Not to be a noodge -- But also, a little grammar goes a long way .. Folks will TRY to read your run-on sentence and just move to the next topic without even looking into your problem. – Zak Sep 16 '22 at 16:21
  • I am sincerely sorry, like I said it was late I will try to be better in explaining next time. – Bhavya Khatri Sep 18 '22 at 04:23

2 Answers2

2

One issue is that you read in arr via:

arr = int(input("Enter the size of the list "))

But then you attempt, later, to call len() on arr:

n = len(arr)

So, since arr is already an int, it has no length. It is a single item.

I think you meant to use num_list and not arr. If that's the case, then the last two lines of your program should be:

n = len(num_list)
EvenProduct(num_list, n)

Especially since, in num_list, you are referring to arr[i] which is invalid if arr is an int. I think you were assuming that arr was num_list, or maybe at some point you changed the name.

Random Davis
  • 6,662
  • 4
  • 14
  • 24
0

You meant to write n = len(num_list).

The value arr is an integer so, as the error indicates, it has no length.

There's no need to pass the length into the function, you can calculate it inside with len(arr).

You can use the enumerate global function to get both index and value simultaneously.

#!/usr/bin/env python

def evens_product(l):
    product = 1
    for i,v in enumerate(l):
        if i % 2 == 0:
            product *= v
    return product

result = evens_product([1,2,3,4,5,6])
print(result) # 15
kwiknik
  • 570
  • 3
  • 7