Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Questions asking for code must demonstrate a minimal understanding of the problem being solved. Include attempted solutions, why they didn't work, and the expected results. See also: Stack Overflow question checklist
Closed 9 years ago.
Improve this question
I have the following code:
p=3.14159
#Function to circleArea
def circleArea(A):
A=r*r*p
radius=r
return radius
#Main Program
r=int(input("Enter radius: "))
print("The area of the circle is:" )
print(circleArea(A))
Why does the line beneath yield this error?
print(circleArea(A))
NameError: name 'A' is not defined
It seems you want to return r*r*p from circleArea() function and pass r as an argument. Also, you may want to use math.pi instead of hardcoding it's value:
import math
#Function to circleArea
def circleArea(r):
return r * r * math.pi
#Main Program
r=int(input("Enter radius: "))
print("The area of the circle is:" )
print(circleArea(r))
DEMO:
Enter radius: 10
The area of the circle is:
314.159265359
In your function definition, A is just a local variable inside the function. The variable that you're wanting to pass in is actually called r. So, instead, you want:
print(circleArea(r))
You also want to change your function declaration to:
def circleArea(r):
Related
Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed last month.
This post was edited and submitted for review last month and failed to reopen the post:
Original close reason(s) were not resolved
Improve this question
I have a variable which has an equation .I'm trying convert the equation that the varibale has into a string and compute the results
Eg
def func1(x):
y = x + 5
return y, 'x+5'
x as input can vary since I'm iterating
through multiple values
Say
h[0][1] = 5
func1(h[0][1])
Output - 10, "h[0][1]+5"
Required result
I need x+5 as string and the computed result of y as a while calling func1
Eval and exec seemed like a probable solution but they perform the inverse of what is needed
I'm not sure I understand why you'd want this but given that the variable holding the equation would be known when coding, you could just wrap the equation in a function. Eg:
def add_five(x):
return (x + 5, "x + 5")
x = 10
y = add_five(x)
print("Answer is", y[0])
print("Equation is", y[1])
Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 1 year ago.
Improve this question
i have a lab for my computer science class and i can't seem to get it right.
we're required to import everything from math and to define functions for geometric shapes
ex:
def triangleArea(b,h):
return b * h / 2
for calling it, we need to make a menu to make the user choose what they want for these geometric shapes. for example, if you chose the number 2 and it gives you the volume of a cylinder.
i just can't seem to understand how you call the function inside the while/for loops.
thank uu~!
def find_area(width, height): # define function
return width * height
for i in range(10):
area = find_area(i, 10) #calls function 'find_area()' and saves returned
#value to variable area
print(area) # prints area
count = 0
while count < 10:
area = find_area(i, 10) #calls function 'find_area()' and save returned
#value to variable area
print(area)
count += 1
Example calling function find_area() from within a for-loop and while-loop.
Closed. This question needs debugging details. It is not currently accepting answers.
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.
Closed 2 years ago.
Improve this question
I'm trying to make an average but for some reason when I try to make one it doesn't work.
I have global variables and array defined at the begining of my document :
vent_moyenne_km = []
compteur_moyenne=0
I have one of my function that is called every X time. In that one, I calculate a velocity with some value that are display on a label of my interface. that part is working, but not the mean
global compteur_moyenne
compteur_moyenne += 1
ventkmh = (vent_1[3][0]*256 + vent_1[4][0]) /100 *3.6
label_vent2_2.config(text= "%.2f" % ventkmh)
vent_moyenne_km.append("%.2f" % ventkmh)
vent_1.clear()
if compteur_moyenne == 5:
compteur_moyenne = 0
print(vent_moyenne_km)
label_vent4_2.config(text=statistics.mean(vent_moyenne_km))
vent_moyenne_km.clear()
of course in my imports I have :
import statistics
When I comment the line label_vent4_2.config(text=statistics.mean(vent_moyenne_km)), everything works and I see in the terminal my array with 5 values. I also tried numpy and even tried to make a for items in array: then add then manually, and everytime I get the error : class 'IndexError'
I'm really not sure how to fix that.
For calculating an average of a list just use numpy:
def function():
value = random.randint(0,1)
return value
list = []
for i in range(100):
list.append(function())
if i%5 == 0:
print(np.average(list))
Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 4 years ago.
Improve this question
I am trying to identify what the problem with the differentiation of trig functions in Python. I use scipy.misc.derivative.
Correct case:
def f(x):
return math.sin(x)
y=derivative(f,5.0,dx=1e-9)
print(y)
This will give a math.cos(5) right?
My problem is here. Since python accepts radians, we need to correct what is inside the sin function. I use math.radians.
If I code it again:
def f(x):
return math.sin(math.radians(x))
y=derivative(f,5.0,dx=1e-9)
print(y)
This will give an answer not equal to what I intended which should be math.cos(math.radians(5)).
Am i missing something?
You have to be consistent with the argument of the trigonometric function. Is not that "Python accepts radians", all programming languages I know use radians by default (including Python).
If you want to get the derivative of 5 degrees, yes, first convert to radians and then use it as the argument of the trigonometric function. Obviously, when you do
y=derivative(f,5.0,dx=1e-9)
using
def f(x):
return math.sin(x)
you get f'(x)=cos(x) evaluated at 5 (radians). If you want to check that the result is correct this is the function to check, not f'(x)=cos(math.radians(x)), which will give you another result.
If you want to pass 5 degrees, yes, you will need to get the radians first:
y=derivative(f,math.radians(5.0),dx=1e-9)
which will be the same as cos(math.radians(5)).
Here is a working example
from scipy.misc import derivative
import math
def f(x):
return math.sin(x)
def dfdx(x):
return math.cos(x)
y1 = derivative(f,5.0,dx=1e-9)
y2 = dfdx(5)
print(y1) # 0.28366
print(y2) # 0.28366
Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
This question appears to be off-topic because it lacks sufficient information to diagnose the problem. Describe your problem in more detail or include a minimal example in the question itself.
Closed 8 years ago.
Improve this question
I have a math problem where I substitute x with numbers from 1 to 25. I did this using a for loop with x in range(1,26). This prints out a list of floats, as it should. After that, I am supposed to print the smallest number found between this range. I have tried using "min()" but I get an error saying that float object is not iterable. Can someone help me figure out a way to print the smallest value?
Min() takes a list, not a float. You don't need a for loop.
myNums = [1.234, 2.345, 4.543]
print min(myNums)
Otherwise if for your math problem you have to use a loop:
myNums = [1.234, 2.345, 4.543]
min = myNums[0] #initial low
for num in myNums:
if num < min:
min = num
print min