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)
Related
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'm trying to write a weight converter which splits the given input and act accordingly however if statement is completely ignored. So far my search revealed that it might be a scope problem but I'm pretty green so couldn't apply any solutions to my specific code.
def weight_convert(answer):
seperated = answer.split(" ")
unit = seperated[1]
weight = int(seperated[0])
if unit.lower == "lbs":
converted = weight * 0.45
else:
converted = weight % 0.45
return converted, unit
Try this:
def weight_convert(answer):
seperated = answer.split(" ")
unit = seperated[1]
weight = int(seperated[0])
if unit.lower() == "lbs":
converted = weight * 0.45
else:
converted = weight % 0.45
return converted, unit
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 3 years ago.
Improve this question
I'm trying to write a function that takes in a dictionary and multiplies a specific value by 2.
EDIT: The below appears to be a simple error. Instead of return getdoubleage(profile["age"]), it would be return profile["age"].
Caleb Goodman and S Vengat answered the issue below, if anyone is running into the same issues.
def getdoubleage(profile):
return getdoubleage(profile["age"]) * 2
print(getdoubleage({"name": "Bob", "age": 10})) # => 20
print(getdoubleage({"name": "Steve", "age": 20})) # => 40
I'm expecting [below] for my outputs.
20
40
This is the TypeError I'm receiving.
File "/Users/peteryoon/PycharmProjects/Test3/Test3.py", line 3, in getdoubleage
return getdoubleage(profile["age"]) * 2
TypeError: 'int' object is not subscriptable
You made a recursive function, and the only reason you don't get a RecursionError, is because a TypeError is thrown first.
Think about what is happening:
First you pass a dictionary to getdoubleage, then you find pass the value for the "age" key (which is 10), to getdoubleage again. Now the code for getdoubleage starts over from the beginning of the function, and tries to run return getdoubleage(10["age"]) * 2, which will always throw a TypeError because you can't use [] on integers.
To fix this simply return the value that you get from the dictionary:
def getdoubleage(profile):
return profile["age"] * 2
def getdoubleage(person):
person["age"] *= 2
return person
Think this works.
Edit: To explain the error you got, your original dictionary is used to call the same function, but with the value associated with the age attribute. Then your profile variable when called a second time is of type 'int', and as you know, a[b] does not work if a is a non-indexable like an integer.
Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Closed 5 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
def vector(sample_list):
for item in sample_list:
sums = 0
square = item**2
sums = square + sums
magnitude = sums**0.5
return magnitude
print(vector([2,3,-4]))
Why this code doesn't give the correct magnitude?
It gives the last value of the vector in function call.
change sums=0 position
def vector(sample_list):
sums = 0
for item in sample_list:
square = item**2
sums = square + sums
magnitude = sums**0.5
return magnitude
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.
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