Home > Blockchain >  I'm new in python, how to solve my code problem of TypeError: missing 2 required positional arg
I'm new in python, how to solve my code problem of TypeError: missing 2 required positional arg

Time:09-19

I write this function of "Euler2quat" in python, it seems okay but when I call it to perform the conversion in this line "q0123_0 = Euler2quat([ptp0])" of my code I received this TypeError: Euler2quat() missing 2 required positional arguments: 'theta' and 'psi'.... I tried to define them like this also.

def Euler2quat(phi_theta_psi):
    phi = phi_theta_psi(1)
    theta = phi_theta_psi(2)
    psi = phi_theta_psi(3)

but still, have another new error: TypeError: 'list' object is not callable

it's my first time using python, I need help with this thanks in advance.

here is the code.

import numpy as np
import matplotlib.pyplot as plt
import scipy.integrate as sci

def Euler2quat(phi, theta, psi):
   
    q0 = np.cos(phi/2) * np.cos(theta/2) * np.cos(psi/2)   np.sin(phi/2) * np.sin(theta/2) * np.sin(psi/2)
    q1 = np.sin(phi/2) * np.cos(theta/2) * np.cos(psi/2) - np.cos(phi/2) * np.sin(theta/2) * np.sin(psi/2)
    q2 = np.cos(phi/2) * np.sin(theta/2) * np.cos(psi/2)   np.sin(phi/2) * np.cos(theta/2) * np.sin(psi/2)
    q3 = np.cos(phi/2) * np.cos(theta/2) * np.sin(psi/2) - np.sin(phi/2) * np.sin(theta/2) * np.cos(psi/2)
    
    return[q0,q1,q2,q3]

## Initial conditions for Attitude and Angular velocity 
phi0 = 0
theta0 = 0  
psi0 = 0
ptp0 = np.asarray([[phi0],[theta0],[psi0]])
q0123_0 = Euler2quat([ptp0])

CodePudding user response:

import numpy as np
# import matplotlib.pyplot as plt
# import scipy.integrate as sci


def Euler2quat(ptp):
    phi, theta, psi = ptp
    q0 = np.cos(phi / 2) * np.cos(theta / 2) * np.cos(psi / 2)   np.sin(phi / 2) * np.sin(theta / 2) * np.sin(psi / 2)
    q1 = np.sin(phi / 2) * np.cos(theta / 2) * np.cos(psi / 2) - np.cos(phi / 2) * np.sin(theta / 2) * np.sin(psi / 2)
    q2 = np.cos(phi / 2) * np.sin(theta / 2) * np.cos(psi / 2)   np.sin(phi / 2) * np.cos(theta / 2) * np.sin(psi / 2)
    q3 = np.cos(phi / 2) * np.cos(theta / 2) * np.sin(psi / 2) - np.sin(phi / 2) * np.sin(theta / 2) * np.cos(psi / 2)

    return [q0, q1, q2, q3]


## Initial conditions for Attitude and Angular velocity
phi0 = 0
theta0 = 0
psi0 = 0
ptp0 = np.asarray([[phi0], [theta0], [psi0]])
q0123_0 = Euler2quat(ptp0)

CodePudding user response:

Your second error is because you can't call lists like functions. What are you trying to do there, get the items of the list into separate variables? Indexing uses [] and furthermore, list indexes start at 0.

def Euler2quat(phi_theta_psi):
    phi = phi_theta_psi[0]
    theta = phi_theta_psi[1]
    psi = phi_theta_psi[2]

But the more Pythonic way to write it is to use unpacking:

phi, theta, psi = phi_theta_psi

CodePudding user response:

Euler2quat function requires 3 arguments, phi, theta and psi. You are calling it as Euler2quat([ptp0]), which means passing just one argument, a list with one element, ptp0.

You should call Euler2quat(phi0,theta0,psi0) instead.

Also, as mentioned by @stian-larsen, when accessing lists you should use squared brackets.

CodePudding user response:

As pointed out by @Trenomarcus, your function requires 3 arguments, the code below should work: q0123_0 = Euler2quat(*ptp0)

CodePudding user response:

phi_theta_psi(1) i a Tuple, not a list. try using squared brackets instead, like this: phi_theta_psi[1]. Happy coding

CodePudding user response:

The solution to both problems is pretty simple and the maths seems sound.

The reason your initial Euler2quat function wasn't working because the function takes 3 arguments (phi, theta and psi), each of which are different objects.

Note: arrays are called lists in python (outside of libraries such as numpy), so I will be referring to them as lists for the entirety of this answer.

You tried to call this function with a list/array (ptp0), but a list is considered as one object. A list is a just a collection of data (in your case, numerical data) but it can also be considered as 1 piece of data (sequential data) which can be indexed as I think you tried to do with your psi_theta_psi(1). When indexed, it needs to be indexed with square brackets ([]) rather than circular ones, since circular brackets are used for calling functions and classes whilst square brackets are used for indexing and slicing. Your corrected function wouldn't have worked for a few other reasons, but the square brackets would remove the TypeError: 'list' object is not callable error.

To remove the original error, just call Euler2quat like so: Euler2quat(psi0, theta0, psi0) or if you need to use php0 to call the function, you can use Euler2quat(*php0). This works because * can be used to unpack iterators (i.e. to return all objects inside one object (such as a list object) as multiple objects), and Euler2quat requires 3 objects in it's arguments. Using Euler2quat([php0]) doesn't work because it still calls on the function with 1 argument, which is a list containing php0. Putting one list inside another list doesn't unpack it, it just creates a list of lists (i.e. a 2d array). Also when using return in a function it's best to use it with a space between it and the data it returns.

The function from your second try wouldn't have worked for many reasons as I said above, the first reason being that it creates a completely new function for Euler2quat which only tries to unpack lists and completely overrides the original. I think what you wanted to do was to create something similar to a decorator function, but giving that function the same name as one which already exists means essentially deleting and replacing the function which already exists with that name. Secondly, when indexing lists, the first value in a list has an index of 0 and the last has an index of it's length - 1 (this is a python thing), meaning that all phi_theta_psi[n] (where n represents the index number you used) lines would need to be changed to phi_theta_psi[n-1].

Lastly, python is all about readability so I don't recommend putting a 0 at the end of all your variable names (because it isn't necessary), and names like q0123_0 or Euler2quat don't promote readability.

I suggest doing some more learning on python, because your code is riddled with errors and you need to learn a programming language's rules and syntax before using it, similarly to how you would with a verbal language like Spanish. Python may be the easiest language to learn, but you can't expect to type anything and for it to work.

Thank you for coming to my Ted Talk.

  • Related