Jump to content


Check out our Community Blogs

Register and join over 40,000 other developers!


Recent Status Updates

View All Updates

Photo
- - - - -

Python : Using a list in a function to print out results

python lists

This topic has been archived. This means that you cannot reply to this topic.
1 reply to this topic

#1 mastermind1

mastermind1

    CC Lurker

  • New Member
  • Pip
  • 4 posts

Posted 25 October 2014 - 08:24 AM

def function(r):
    
    r = 6
    c = 10
    p = 0
    h = 0
    ht = 0.2
    o = 0.5
    l = 7
    a= [1.0,2.0,3.0]
    ol = [o * l * x for x in a]
    
    pp = zeros(0)
    aa = zeros(0)
    cc = zeros(0)
    pp = append(pp,p)
    aa = append(aa,x)
    cc = append(cc,c)

    while p >= 0 and h < 9:
        
        h = h + ht
        p = p + ol * ht
        result = r * cc * ht
        
    return result

ans = function(r)
print ans

Okay. I do not know for some reason, this program is giving me a TypeError. I do not know what it means. Please tell me what change I need to make to get rid of this error and get something to print out. I want ol to take the assigned o and l value and multiply each other and with each of the value in the list a. The code itself is self-explanatory from there. I want to do the calculation result = r * cc * ht and return the value.The error is below:

---------------------------------------------------------------------------
TypeError                                 Traceback (most recent call last)
<ipython-input-41-4870caf6da2e> in <module>()
     26     return result
     27 
---> 28 ans = function(r)
     29 print ans

<ipython-input-41-4870caf6da2e> in function(r)
     21 
     22         h = h + ht
---> 23         p = p + ol * ht
     24         result = r * cc * ht
     25 

TypeError: can't multiply sequence by non-int of type 'float'

Thanks!!



Edited by mastermind1, 25 October 2014 - 08:25 AM.


#2 gonerogue

gonerogue

    CC Addict

  • Advanced Member
  • PipPipPipPipPip
  • 197 posts

Posted 25 October 2014 - 09:26 AM

ht is a float.

ol is a list.

 

Here: p = p + ol * ht

you are multiplying ol and ht.

But it is not allowed to multiply a list and a float, it makes no sense at all ... hence the error.


Edited by gonerogue, 25 October 2014 - 09:26 AM.





Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download