syntaxerror in code but not in line of code [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
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 - - - - - - - - - - - - - - - - - - - - - - - - ^

Related

Running into a print problem with floats and integers [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 10 months ago.
Improve this question
I am trying to create a program where the user is asked to input their credit score and based upon if it is less than or = to 650, they will have to put either 10% or 20% downpayment on the house. The problem I'm having is when running my code instead of printing the downpayment amount it just prints the decimal "0.1" or "0.2" multiple times as shown below. I've included my simple program below.
credit_score = float(input("What is your credit score? "))
house_price = 1000000
if credit_score <= 650:
print('down_payement = ' + str(0.2) * house_price)
else:
print('down_payement = ' + str(0.1) * house_price)
Convert to string after multiplication:
credit_score = float(input("What is your credit score? "))
house_price = 1000000
if credit_score <= 650:
print('down_payement = ' + str(0.2 * house_price))
else:
print('down_payement = ' + str(0.1 * house_price))

I am getting "TypeError: '>' not supported between instances of 'float' and 'str'" when I finish this code. What am I doing wrong? [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
Here is the code
wagesAmount = float(input("Enter Wages Amount: "))
numberOfHours = float(input("Enter Number Of Hours Worked: "))
totalWagesAmount = (wagesAmount) * (numberOfHours)
if totalWagesAmount > "0":
printtotalWagesAmount
I need to add an else statement as well but I was just trying to troubleshoot this issue first
"0" means it's a string . so when u wanna do if something > 0: thats means that 0 is integer and you say (if somthing > "0" string. so you
wagesAmount = float(input("Enter Wages Amount: "))
numberOfHours = float(input("Enter Number Of Hours Worked: "))
totalWagesAmount = (wagesAmount) * (numberOfHours)
if totalWagesAmount > 0:
print (totalWagesAmount)
You have to remove the "" on 0 because "0" != 0
if totalWagesAmount > 0:
Writing "0" means its string because of dynamic typing in python.

How to fix a syntax error when trying to output two items [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 3 years ago.
Improve this question
trying to get the radius of a circle as an output while inputting the area but I keep getting a syntax error in the last line. please help this keeps happening on alot of my programs when I try to output two items.
I've tried many solutions that I know
area = int(input ("Enter area of a circle: "))
pi = int(3.1416)
radius = (area/pi) ** (1/2)
print ("Radius of a circle with area", area "is", radius)
I keep getting a invalid syntax error right after the "is" in the last line before the , radius.
You're missing a comma between area and "is" in the print function.
int(3.1416) is equal to 3, use math.pi instead of defining your own pi.
Use math.sqrt instead of ** (1 / 2).
Use float instead of int for the area.
from math import pi, sqrt
area = float(input("Enter area of a circle: "))
radius = sqrt(area / pi)
print ("Radius of a circle with area", area, "is", radius)

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

Program for approximating pi [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
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

Categories

Resources