Program for approximating pi [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 7 years ago.
Improve this question
The question asks to write a program that approximates pi using the forumla pi = 4/1 - 4/3 + 4/5 - 4/7 + .....
I have to prompt the user for the number of terms in the series, and the calculate the approximation. This is the program I tried
import math
def main():
dummy = 4.0
term = 0.0
n = 0.0
print("This program approximates the value of pi")
n = eval(input("Enter the number of terms you want in the approximation: ")
for i in range(1, n+1)
term = 4/(2n+1)
dummy = dummy + ((-1)**n) * term
print("The approximation is ", dummy)
print("The difference between pi and the approximation is ", math.sqrt((math.pi - dummy)**2))
However, when I try to run it I get the error message "Invalid syntax" and the variable "term" is highlighted in red.

Missing operator * here:
term = 4/(2n+1)
change to
term = 4/(2*n+1)

You forgot the colon at the end of this line:
for i in range(1, n+1):
^ missing
Whenever you get a syntax error on a line that looks correct, try looking at the line before the error to see if that's correct.
You also forgot the ) at the end of this line:
n = eval(input("Enter the number of terms you want in the approximation: ")

You miss ":" at the end of the for statement:
for i in range(1, n+1):
term = 4/(2*n+1)
dummy = dummy + ((-1)**n) * term

Related

Invalid Syntax on variables 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 last year.
Improve this question
May I know why there be a invalid syntax on my f variable on the first elif loop?
def bsd():
if price_of_properties <= 180000:
price = 180000*0.1
f = '{0:.2f}'.format(price)
print("BSD is $" + str(f))
elif price_of_properties > 180000 <= 360000:
price = (((180000*0.1) + (price_of_properties - 180000 + (180000* 0.2)))
f = '{0:.2f}'.format(price)
print("BSD is $" + str(f))
elif price_of_properties > 360000 <= 100000:
price = (((180000*0.1) + (180000*0.2) +(price_of_properties - 180000 + (640000* 0.3)))
f = '{0:.2f}'.format(price)
print("BSD is $" + str(f))
Your second and third assignments to the price variable have unbalaned parentheses ((more opens than closes).
This will cause an error on the following lines though I think Python 3.11 may fix that, reporting errors more accurately. You can probably fix this by adding a closing parenthesis on each of those two lines. Or removing the first. But check the formulae after you do so, in case you make the wrong choice.
And, yes, the extra opening parenthesis in that first paragraph was intended :-)

im trying to find the volume of a shape but the definition is not working [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 4 years ago.
Improve this question
i want the code to ask for the radius and length then the program will calculate piradiusradius*length from the definition area the print out the radius for example if at asks me the radius i put 5 same with the length it should say the volume is 392.75
def calculate_volume(radius, length):
pi=3.142
vol=pi*radius*radius*length
#main program
radius = int(input("enter radius: "))
length = int(input("enter length: "))
volume = calculate_volume(radius, length)
print("the volume: ",volume)
You are missing the return statement:
def calculate_volume(radius, length):
pi = 3.142
vol = pi * radius * radius * length
return vol
The example is almost correct, you just haven't returned a result from your function!
def calculate_volume(radius, length):
pi=3.142
vol=pi*radius*radius*length
return(vol)
When you put volume = calculate_volume(radius, length) right now volume is being set to None instead of vol from inside of the function.
Or better yet -- Get in the habit of using type hints - then use a decent IDE (such as PyCharm), or a type checker (such as MyPy). PyCharm would have given you a warning that you didn't have a return statement -- and anything calling your function would know the types of parameters and the return type.
def calculate_volume(radius: float, length: float) -> float:
pi = 3.142
vol = pi*radius*radius*length
return vol

Python Iteration Homework [closed]

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 6 years ago.
Improve this question
I have 2 questions I need to ask for help with. One question I don't entirely understand so if someone could help me to that would be great.
Question One (the one I don't entirely understand):
One definition of e is
Formula for e
This can be calculated as my_e = 1/math.factorial(0) + 1/math.factorial(1) + 1/math.factorial(2) + 1/math.factorial(3) + …
Let n be the input number of the math.factorial() function. n successively takes on 0, 1, 2, 3 and so on. Find the smallest n such that the absolute value of (my_e – math.e) is less than or equal to 10-10. That is, abs(my_e - math.e) <= (10 ** -10).
I just don't entirely understand what I am being asked to do. Clarification would be great. Thanks!
Question 2:
Ask the user to type in a series of integers. Sum them up and print out the sum and the number of integers the user has entered.
My code
So what should happen is after I enter the numbers I want to enter and hit the enter key, it should calculate and print out "sum = 25 count = 3". The screenshot shows what error message I am getting.
Any help you have is welcomed and greatly appreciated.
As far as you first question goes:
>>> import math
>>> math.e
2.718281828459045
>>> sum(1.0/math.factorial(i) for i in range(5))
2.708333333333333
>>> abs(sum(1.0/math.factorial(i) for i in range(5)) - math.e) < 10**-10
False
>>> abs(sum(1.0/math.factorial(i) for i in range(30)) - math.e) < 10**-10
True
So, somewhere between n == 5 and n == 30 you get 10 decimal places for e. Create the sum term by term in a while-loop (instead of by using the sum function as I have, since you are unlikely to have seen that syntax yet). At each pass through the loop, compare the sum with math.e. Stop when you get the target accuracy. Return the final n.

How do i figure out the multiple of a number python [closed]

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 6 years ago.
Improve this question
ok, I am studying python in my computing course and i have been challenged with designing a code which validates a GTIN-8 code, I have came to a hurdle that i can not jump. I have looked on how to find the equal or higher multiple of a 10 and i have had no success so far, hope you guys can help me!
Here is a small piece of code, i need to find the equal or higher multiple of 10;
NewNumber = (NewGtin_1 + Gtin_2 + NewGtin_3 + Gtin_4 + NewGtin_5 + Gtin_6 + NewGtin_7)
print (NewNumber)
The easiest way, involving no functions and modules, could be using floor division operator //.
def neareast_higher_multiple_10(number):
return ((number // 10) + 1) * 10
Examples of usage:
>>> neareast_higher_multiple_10(15)
20
>>> neareast_higher_multiple_10(21)
30
>>> neareast_higher_multiple_10(20.1)
30.0
>>> neareast_higher_multiple_10(20)
30
We can also make a generalized version:
def neareast_higher_multiple(number, mult_of):
return ((number // mult_of) + 1) * mult_of
If you need nearest lower multiple, just remove + 1:
def neareast_lower_multiple(number, mult_of):
return (number // mult_of) * mult_of
To find the nearest multiple, you can call both these functions and use that one with a lower difference from the original number.

syntaxerror in code but not in line of code [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
So I'm trying to program something and there's something odd going on, when trying to run my code I get a syntaxerror on line 3 but when I run only that specific line of code everything works perfectly. How is this even possible? It's written in python if that matters
p = float(input("What is the initial investment? "))
r = float(input("What is the nominal interest rate as a decimal? ")
n = float(input("How many times a year will the interest be compounded? "))
t = float(input("How many years will the interest be compounded"))
a = p * (1 + r / n) ** (n*t)
print "The final amount after %f years is %f" %t %a
Try this, (missed brackets in 2nd line).
r = float(input("What is the nominal interest rate as a decimal? "))
You are missing a closing bracket on line 2. Correct that, and the error should go away.
#Instead of
r = float(input("What is the nominal interest rate as a decimal? ")
#Do this
r = float(input("What is the nominal interest rate as a decimal? "))
#This guy is missing - - - - - - - - - - - - - - - - - - - - - - - - ^

Categories

Resources