Python: more elegant way to assert type of function input

I am a complete beginner in Python… Currently I am trying to write a function which first checks whether the inputs x and y are int or float. My guess which does the job is if (type(x) != int and …

The gradient (2d) search function returns None. What is the problem?

I have a function that should return a gradient. def numerical_derivative_2d(func, epsilon): def grad_func(x): grad_func = (func(x + np.array([epsilon, 0])) – func(x)) / epsilon, (func(x + …

Using the output of a function as an input of another function

Suppose i have function like this. def funA(a,b): if a>5: c=a+b if a<5: c=a-b return c So is it possible for me to use the variable C outside in another function like this? Is this a ...

‘BACKTICK_QUOTED_STRING__AT_key’ is not defined for Pandas Quering function

Trying to get a hold of pd.query function Getting a UndefinedVariableError: name ‘BACKTICK_QUOTED_STRING__AT_key’ is not defined for the below pandas python code. Where am i going wrong? Answer Try this : or

Python 3: Calculating percentage change with n number of values

I have a function which should calculate percentage increase / decrease between two values. The parameters in the function are original_value and new_value. Problem: I need the program to take n number of values, for example, six values, and then run the function on every pair of values. An example: The values are 1, 2, 3, 4, 5 and 6. I then need the program to calculate the avg. percentage change between 1 and 2, 2 and 3, 3 and 4, 4 and 5, and 5 and 6. I thought this would be best by making the aforementioned function, then

Python – How to create a delete function for user input?

I’ve been trying to create a program which allows users to view a text file’s contents and delete some or all of a single entry block. An example of the text’s file contents can be seen below: As such, the entry block refers to all lines without a horizontal space. Currently, my progress is at: The delete_function() is meant to delete the creature by: Name, which deletes the entire text block associated with the name Information, which deletes only the line of information I can’t seem to get the delete_creature() function to work, however, and I am unsure of how

Counting the number of triplets forming GP series in python

The problem statement is: You are given an array and you need to find number of triplets of indices(i,j,k) such that the elements at those indices are in geometric progression for a given common ratio, r and i<j<k. For example, arr=[1,4,16,64]. If r=4, we have [1,4,16] and [4,16,64] at indices (0,1,2) and (1,2,3). I have a solution but I am unable to understand it. Can anyone help me out? Answer The way this problem works is that it uses “dict” to keep a running count of each number in the array. Then, it uses “dictPairs” to keep a running count

Call many python functions from a module by looping through a list of function names and making them variables

I have three similar functions in tld_list.py. I am working out of mainBase.py file. I am trying to create a variable string which will call the appropriate function by looping through the list of all functions. My code reads from a list of function names, iterates through the list and running the function on each iteration. Each function returns 10 pieces of information from separate websites I have tried 2 variations annotated as Option A and Option B below Option A Option B Even though it is not seen, I continue to loop through the iteration by ending each loop

List and use the variables defined inside my own function

Suppose that I have a function like this one: Now can I use the a, bvariables? Even if foo() is not returning them like this? I know this is easy to do if foo was a class, but it’s not. Answer Even though you are saying you have a function, not a class, python is able to transcend those differences using callable classes. Those are normal classes whose instances can be called as normal functions while still maintaining an internal state. Here is a demonstration: In the above code snippet, you can think of Foo as a class that generates

how to access a dictionary from another function in the current function

I am trying to refer to the dictionary in function_one. I have tried to return the dictionary variables, and use the dictionary names as parameters and arguments. However, I am getting an error message saying that the dictionaries I am trying to access in function_two is not defined. Here is my simplified code: Basically, I am asking if the element chosen for input1 is in the dictionary in the previous function I want the program to change the total_cost value etc. Answer When you are calling the function_one(), you are not using the dictionaries returned by it. You can use