0

I'm stumbled into the same error over and over again. I define multiple functions but I'm having trouble with this part:

a = 50000.0
c = 1.0
def fkt(smatrix, a, c):
    y_Dach = [0.] * 93
    for i in range(0, 93):
        y_Dach[i] = c*( 1.5*(smatrix[i])/a -
        0.5*((smatrix[i])/a)**3.0 )
    return y_Dach

So smatrix is a list with 93 floats - 1 dimensional, which is proven by the fact that I tried recalling it and the type with print smatrix[0] and print type(smatrix[0]) inside the function already - and it works as expected.

My problem is that I keep gettin this error:

Traceback (most recent call last):
    File "bla.py", line 260, in <module>
        print anneal(fkt, a, c, bmatrix)
    File "bla.py", line 242, in anneal
        fkt(smatrix, a, c)
    File "bla.py", line 220, in fkt
        y_Dach[i] = c*( 1.5*(smatrix[i])/a -
        0.5*((smatrix[i])/a)**3.0 )
TypeError: can't multiply sequence by non-int of type 'float'

I literally tried everything my little programming skills can do to fix that, but I can't get rid of the error, it keeps reappearing. Why I can't calculate with only floats inside my list?

I'm pretty new to programming but i try everything to learn more at the moment. I hope I can reply correct to your request, if not let me know. @Anand S Kumar when i call smatrix by print smatrix[0] it will show 2500.0 , when i do print smatrix it will show:

[2500.0, 3500.0, 5500.0, 6500.0, 7500.0, 8500.0, 9500.0, 10500.0,
11500.0, 12500.0, 13500.0, 14500.0, 15500.0, 16500.0,
17500.0, 18500.0, 19500.0, 20500.0, 21500.0, 22500.0, 23500.0, 24500.0,
25500.0, 26500.0, 27500.0, 28500.0, 29500.0, 30500.0, 31500.0, 32500.0,
33500.0, 34500.0, 35500.0, 36500.0, 37500.0, 38500.0, 39500.0, 40500.0,
41500.0, 42500.0, 43500.0, 44500.0, 45500.0, 46500.0, 47500.0, 48500.0,
49500.0, 50500.0, 51500.0, 52500.0, 53500.0, 54500.0, 55500.0, 56500.0,
57500.0, 58500.0, 59500.0, 60500.0, 61500.0, 62500.0, 63500.0, 64500.0, 
65500.0, 66500.0, 67500.0, 68500.0, 69500.0, 70500.0, 71500.0, 72500.0, 
73500.0, 74500.0, 75500.0, 76500.0, 78500.0, 79500.0, 80500.0, 81500.0, 
83500.0, 85500.0, 87500.0, 88500.0, 89500.0, 90500.0, 91500.0, 92500.0,
93500.0, 95500.0, 98500.0, 99500.0, 100500.0, 103500.0, 107500.0]
8
  • 5
    show us how smatrix is defined and how you call the function? Commented Jul 31, 2015 at 14:57
  • 1
    /* BTW [0. for j in range (93)] can be written shorter as [0] * 93 */ Commented Jul 31, 2015 at 14:59
  • 3
    Split your computation on to multiple lines and use local variables. It will make it easier to read, and also to know what the error message indicates. You have two multiplications in that statement, so either one could be the error. I defined smatrix=[1.0,2.0,3.0] and the expression evaluated without error. Commented Jul 31, 2015 at 15:02
  • Since no one has explicitly stated this yet, you're getting that error because something you think is a float is actually a list or other sequence. I feel like that's clear from the exception reason but for some reason a lot of new programmers don't grok the type mismatch exceptions. Commented Jul 31, 2015 at 15:20
  • @9000 You... you commented your comment? Commented Jul 31, 2015 at 15:20

2 Answers 2

2

The only time you can "multiply" a list by a scalar value is what you used early to create a row; a list times an int performs repeated concatenation. No other list/scalar operations are defined (including swapping the order of the elements: 3 * [1, 2, 3] is undefined, and not identical to [1, 2, 3] * 3).

In order to perform operations like 1.5*smatrix[i]/a on a row, you need to either do them one element at a time yourself:

new_row = []
for v in smatrix[i]:
    new_row.append(1.5*v/a)

or use a package like numpy that provides an API for scalar/matrix/vector operations.

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

1 Comment

Thanks a lot, you probably pointed out the root of the problem. Now that I'm beginning to understand the problem, I also understand what dsh meant above. I try to fix my code and when I'm done, I'll put the working code in another comment for future users.
0

I'm always getting new inspirations from people like you chepner. You really nailed the problem. Thanks a lot!!! I made it work by using numpy as you suggested.

y_Dach[i] = np.multiply(c,( np.multiply(1.5,((smatrix[i])/a))
- (np.multiply(0.5,(((smatrix[i])/a)**3.0)))))

Let's see how I get on with my Code and future problems. I hope that I'm not running into one anytime soon again. :)

Comments

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.