Vpython Error: 'float' object has no attribute '_x' - physics

I am writing a Vpython simulation for projectile motion and keep getting the error ('float' object has no attribute '_x') on this line(ball.vel.y = ball.vel.y + g*dt) . I have tried changing the values of ball.vel.y to an integer and changing g to an integer but the same error occurs. Here is the code
from vpython import *
import math
ball=sphere(radius=0.1, color=color.red, pos=vector(0.1,0.1,0),make_trail=True)
floor=box(pos=vector(0,0,0), length=10, height=0.01, width=0.01)
g= vector(0,-9.8 ,0)
ball.vel=vector(10*cos(43),10*sin(43),0)
dt=0.1
t=0.0
while(ball.pos.y>-0.001):
rate(100)
t=t+dt
ball.pos.x = ball.pos.x + ball.vel.x*dt
ball.vel.y = ball.vel.y + g*dt
ball.pos.y = ball.pos.y + ball.vel.y*dt

g is a vector, as is g*dt, but ball.vel.y is a scalar, and you can't add a vector to a scalar. It's unfortunate that the error message doesn't just say "You can't add a vector to a scalar". I note that if you reverse the two quantities the error message is a bit more understandable: TypeError: unsupported operand type(s) for +: 'vpython.cyvector.vector' and 'float'

Related

How do I solve this error "An argument of 'series float' type was used but a 'input float' is expected" in Pine Script Version 5

I'm writing a Pine Script Indicator and I'm getting 4 errors and one warning after compiling:
Cannot call 'hline' with argument 'price'='upperBound'. An argument of 'series float' type was used but a 'input float' is expected
Cannot call 'hline' with argument 'price'='lowerBound'. An argument of 'series float' type was used but a 'input float' is expected
Cannot call 'fill' with argument 'hline1'='upperBound'. An argument of 'series float' type was used but a 'hline' is expected
Cannot call 'fill' with argument 'hline2'='lowerBound'. An argument of 'series float' type was used but a 'hline' is expected
(1) warning: The transp argument is deprecated. We recommend using color.new() or color.rgb() functions to specify the transparency of the plots instead. Additionally, note that transp has no effect in plots where the color is calculated at runtime
This is my Pine Script Code:
'''
//#version=5
// Define the number of bars to be analyzed for finding clusters
clusterLength = input(title="Cluster Length", defval=100)
// Define the number of standard deviations from the mean to determine the cluster
stdDev = input(title="Number of Standard Deviations", defval=2.0)
// Calculate the mean and standard deviation for the defined number of bars
mean = ta.sma(close, clusterLength)
stddev = ta.stdev(close, clusterLength)
// Plot the upper and lower bounds of the clusters as horizontal lines
upperBound = mean + stddev * stdDev
lowerBound = mean - stddev * stdDev
hline(upperBound, color=color.red, linewidth=2, title="Upper Bound")
hline(lowerBound, color=color.blue, linewidth=2, title="Lower Bound")
// Fill the area between the bounds to visually represent the cluster
fill(upperBound, lowerBound, color=color.gray, transp=70)
'''
I would appreciate if you provide a solution.
Thanks in advance
You cannot use dynamic values in hline().
You can try using plot() or line instead.
And you should be calling the fill() function with plots or hlines. upperBound and lowerBound are just variables.
See the signature below:
fill(hline1, hline2, color, title, editable, fillgaps, display) → void
fill(plot1, plot2, color, title, editable, show_last, fillgaps, display) → void

df.ix not working , whats the right iloc method?

This is my program-
#n= no. of days
def ATR(df , n):
df['H-L'] = abs(df['High'] - df['Low'])
df['H-PC'] = abs(df['High'] - df['Close'].shift(1))
df['L-PC'] = abs(df['Low'] - df['Close'].shift(1))
df['TR']=df[['H-L','H-PC','L-PC']].max(axis=1)
df['ATR'] = np.nan
df.ix[n-1,'ATR']=df['TR'][:n-1].mean()
for i in range(n , len(df)):
df['ATR'][i] = (df['ATR'][i-1]*(n-1) + df['TR'][i])/n
return
A warning shows up
'DataFrame' object has no attribute 'ix
I tried to replace it with iloc:
df.iloc[df.index[n-1],'ATR'] = df['TR'][:n-1].mean()
But this time another error pops up :
only integers, slices (`:`), ellipsis (`...`), numpy.newaxis (`None`) and integer or boolean arrays are valid indices
How to fix this?
Converting code is a pain and we have all been there...
df.ix[n-1,'ATR'] = df['TR'][:n-1].mean()
should become
df['ATR'].iloc[n-1] = df['TR'][:n-1].mean()
Hope this fits the bill

unsupported operand type(s) for -: 'list' and 'list'

import numpy as np
def answer_seven():
counties = census_df[['POPESTIMATE2010','POPESTIMATE2011','POPESTIMATE2012','POPESTIMATE2013','POPESTIMATE2014','POPESTIMATE2015']]
return counties[[counties.max(axis=1)]-[counties.min(axis=1)]].abs().idxmax()
TypeError: unsupported operand type(s) for -: 'list' and 'list'
Above is my code which does not work and I got this error message.
But the code below does work.
import numpy as np
def answer_seven():
counties_df = census_df[census_df['SUMLEV'] == 50][['CTYNAME','POPESTIMATE2010','POPESTIMATE2011','POPESTIMATE2012','POPESTIMATE2013',
'POPESTIMATE2014','POPESTIMATE2015']]
counties_df["MaxDiff"] = abs(counties_df.max(axis=1) - counties_df.min(axis=1))
most_change = counties_df.sort_values(by=["MaxDiff"], ascending = False)
return most_change.iloc[0][0]
It uses max and min function to get the max difference as well which uses a list to subtract another list. Could someone explain to me why my code is not working but this one does? Thanks!
The problem is here -
return counties[[counties.max(axis=1)]-[counties.min(axis=1)]]
You are subtracting two lists, I think the below edit should make it work
return counties[counties.max(axis=1)-counties.min(axis=1)]

converting pyspark dataframe fail on 'None Type' object

I have a pyspark dataframe 'data3' with many columns. I am trying to run kmeans on it except the first two columns, when I run my code , tasks always fails on TypeError: float() argument must be a string or a number, not 'NoneType' What am I doing wrong?
def f(x):
rel = {}
#rel['features'] = Vectors.dense(float(x[0]),float(x[1]),float(x[2]),float(x[3]))
rel['features'] = Vectors.dense(float(x[2]),float(x[3]),float(x[4]),float(x[5]),float(x[6]),float(x[7]),float(x[8]),float(x[9]),float(x[10]),float(x[11]),float(x[12]),float(x[13]),float(x[14]),float(x[15]),float(x[16]),float(x[17]),float(x[18]),float(x[19]),float(x[20]),float(x[21]),float(x[22]),float(x[23]),float(x[24]),float(x[25]),float(x[26]),float(x[27]),float(x[28]),float(x[29]),float(x[30]),float(x[31]),float(x[32]),float(x[33]),float(x[34]),float(x[35]),float(x[36]),float(x[37]),float(x[38]),float(x[39]),float(x[40]),float(x[41]),float(x[42]),float(x[43]),float(x[44]),float(x[45]),float(x[46]),float(x[47]),float(x[48]),float(x[49]))
return rel
data= data3.rdd.map(lambda p: Row(**f(p))).toDF()
kmeansmodel = KMeans().setK(7).setFeaturesCol('features').setPredictionCol('prediction').fit(data)
TypeError: float() argument must be a string or a number, not 'NoneType'
Your error comes from converting the xs to float because you probably have missing values
rel['features'] = Vectors.dense(float(x[2]),float(x[3]),float(x[4]),float(x[5]),float(x[6]),float(x[7]),float(x[8]),float(x[9]),float(x[10]),float(x[11]),float(x[12]),float(x[13]),float(x[14]),float(x[15]),float(x[16]),float(x[17]),float(x[18]),float(x[19]),float(x[20]),float(x[21]),float(x[22]),float(x[23]),float(x[24]),float(x[25]),float(x[26]),float(x[27]),float(x[28]),float(x[29]),float(x[30]),float(x[31]),float(x[32]),float(x[33]),float(x[34]),float(x[35]),float(x[36]),float(x[37]),float(x[38]),float(x[39]),float(x[40]),float(x[41]),float(x[42]),float(x[43]),float(x[44]),float(x[45]),float(x[46]),float(x[47]),float(x[48]),float(x[49]))
return rel
You can create a flag to convert each x to float when there is a missing values. For example
list_of_Xs = [x[2], x[3], x[4], x[5], x[6],etc. ]
for x in list_of_Xs:
if x is not None:
x = float(x)
Or use rel.dropna()

Objective C - Box2d float type casting: multiplying * operand on float32

Please help me in removing the error with explanation
Error on the following line:
_Sprite.position.x = _Body->GetPosition().x * _PhysicsWorld->RATIO;
Error message: Invalid operands to binary expression ('float32 (aka 'float') and 'float32()())
_Body is a B2Body object
_Sprite is a CCSprite object
_PhysicsWorld->RATIO returns float32
If I change the line to:
_Sprite.position.x = _Body->GetPosition().x * (float) _PhysicsWorld->RATIO;
Another error message comes: C-style cast from float32(*)() to float is not allowed.
You probably need to do:
_Sprite.position.x = _Body->GetPosition().x * _PhysicsWorld->RATIO();
Note the trailing '()' after RATIO.
You are multiplying a float (which parses into pointer dereferencing instead) by a function that returns a float (I believe).