How to Send Input as a List Into Function? [closed] - python

Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 5 years ago.
Improve this question
I trying to pass the input to the function but my code has a problem.
I have position with the (x, y) coordinates for each points.
I would be appreciate if someone help me to fix my program.
def distance(ball1,ball2):
d=(((ball2[0]-ball1[0])**2)+((ball2[1]-ball1[1])**2))**(1/2)
print(float(d))
ball1=[2,10]
ball2=[3,5]
distance(ball1,ball2)
Error has been fixed, But the output is not accurate and the result always is 1.
May you please help to fix that too? thanks

The issue is that you are accessing the third element in ball2, with ball2[2]. However, there are only two elements in the list. You need to change it to ball2[1]-ball1[1]:
def distance(ball1=[],ball2=[]):
d=(((ball2[0]-ball1[0])**2)+((ball2[1]-ball1[1])**2))**(1/2)
print(d)

You cannot access the third element.
Code:
def distance(ball1=[],ball2=[]):
d=(((ball2[0]-ball1[0])**2)+((ball2[1]-ball1[1])**2))**(1/2)
print(d)
ball1=[1,1]
ball2=[3,3]
distance(ball1,ball2)

def distance(ball1=[], ball2=[]):
d = (((ball2[0] - ball1[0]) ** 2) + ((ball2[1] - ball2[1]) ** 2)) ** (1 / 2)
print(d)
ball1 = [1, 1]
ball2 = [3, 3]
distance(ball1, ball2)

The 1 result is due to division.
In Python 2 1/2 does integer division with 0 result.
In Python 3 1/2 works computes to 0.5
Just replace 1/2 with 0.5 in your code.

Related

I am Wrote this python code but every time it's give me wrong answer [closed]

Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 2 years ago.
Improve this question
I have wrote this Code for calculates the product of the first natural numbers, but it showing answer 1 every time. I don't where i did mistake?? Can you please help me find out my mistake in this code..
num = 10
i = 0
prod = 1
while i<=num:
i = i+1
prod*prod*i
print(prod)
The problem seems to be on the line prod*prod*i. The product needs to be accumulated and for this it should be exchanged for prod*=i.
The new snippet is:
num = 10
i = 0
prod = 1
while i<=num:
print(i)
i = i+1
prod*=i
print(prod)
Instead of prod*prod*i write prod=prod*i
Here we first take the input of the number of terms.Then we iterate the for loop and multiply the value of x with the initial value(=1).Then we assign the new value to p.
n=int(input('Terms: ')) #specifing the limit
p=1
for x in range(1,n+1):
p=p*x
print(p)

Calculating Percentage in Python [closed]

Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 2 years ago.
Improve this question
I am trying to calculate percentage in Python, by hand this works fine but in python this formula for some reason does not work. I tried multiple ways but just not getting correct results.
getPercent = lambda part, whole: float(1 - (float(part)/float(part))) * 100
Val1= float(3194.15)
Val2= float(3163.84)
getPercent(Val2, Val1)
Output:
0.0
output should be:
0.95
Just use whole instead of part in the denominator.
getPercent = lambda part, whole: float(1 - (float(part)/float(whole))) * 100
Val1= float(3194.15)
Val2= float(3163.84)
getPercent(Val2, Val1)
Try it online!
you haven't used whole in your lambda function
getPercent = lambda part, whole: float(1 - (float(part)/float(whole))) * 100
Val1= float(3194.15)
Val2= float(3163.84)
getPercent(Val2, Val1)
# output 0.9489222484855064
You need to divide by whole. To get just two decimal places:
get_percent = lambda part, whole: float(1 - part / whole) * 100
val_1 = float(3194.15)
val_2 = float(3163.84)
a_float = get_percent(val_2, val_1)
print("{:.2f}".format(a_float))
Returning:
0.95

My code outputs an error, I don't understand why [closed]

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Closed 4 years ago.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Improve this question
I'm trying to calculate something, but the code outputs an error
I have even tried splitting the operation in many single parts, to not do all at one time, I have tried by setting and int() output, but it doesn't work too
import math
x_coo = 20
y_coo = 30
x = 50
y = 80
def distance(x_coo,y_coo,x,y):
dist = math.sqrt((e.x_coo-a.x)**2 + (e.y_coo-a.y)**2)
force = (81*24*25)/ (distance(e.x_coo,e.y_coo, a.x,a.y)^2)
print(force)
Error:
TypeError: unsupported operand type(s) for ^: 'NoneType' and 'int'
You forget to return the result of the operation inside the function:
import math
x_coo = 20
y_coo = 30
x = 50
y = 80
def distance(x_coo,y_coo,x,y):
dist = math.sqrt((e.x_coo-a.x)**2 + (e.y_coo-a.y)**2)
return dist
force = (81*24*25)/ (distance(e.x_coo,e.y_coo, a.x,a.y)^2)
print(force)
You need to return a value from the function distance. Python loves returning values, and if you do not declare what your function returns, Python assumes you want to return None.
Here's what you should have:
def distance(x_coo,y_coo,x,y):
dist = math.sqrt((e.x_coo-a.x)**2 + (e.y_coo-a.y)**2)
return dist
You missed return on distance(), i.e.:
def distance(x_coo,y_coo,x,y):
return math.sqrt((e.x_coo-a.x)**2 + (e.y_coo-a.y)**2)

Reverse of a list of list in Python [closed]

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Closed 4 years ago.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Improve this question
I have been trying to horizontally reverse the matrix in Python.
input 1 0
1 0
output 0 1
0 1
Here is the code...
class Reversedmat(object):
def __list_oflist__(self,mat)
rows = len(mat)
columns = len(mat[0])
for i in range(0, len(mat)):
reversed_mat = ""
for j in range(0, len(mat[i])):
reversed_mat += str(mat[i][j]) + "\t" ## this is where im
print(reverse(reversed_mat))
if __name__ == "__main__":
mat =[[1,0],[1,0]]
print(Reversedmat().__list_oflist__(mat))
I have been trying to do this in Python, but I was not able to achieve. Any help is much appreciated! Thanks
A simple solution:
def reversemat(mat):
return [row[::-1] for row in mat]
print(reversemat([[1,0],[1,0]]))
# [[0, 1], [0, 1]]
besides, you have returned nothing from your function, so you will get None from your second print.

Python Function Codecademy [closed]

Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 7 years ago.
Improve this question
I'm trying to cube the number in this function, but Codecademy says it isn't returning the right result. Could anyone help?
def cube(number):
return number**number
def by_three(number):
if number % 3==0:
return cube(number)
else:
return False
Because it is not a cube. Cube is: number ** 3
Given your cube function, you are doing
n^n
for example, given n = 4, what you are really doing is 4*4*4*4
And it work only on 3 or it's multiple, given the line
if number % 3==0:
In case you are interested in one liner of this function check this out:
def by_three(number):
return number ** 3 if number % 3 == 0 else False
If you are interested in how this is done check ternary operator in python

Categories