Invalid Syntax on variables in python [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 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 :-)

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))

Why is my Python conditional within function 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 2 years ago.
Improve this question
def blood_pressure(systolic, diastolic):
mean_pressure = (systolic + (2 * diastolic)) / 3
risk = float(1.0)
risk_return = round((risk * mean_pressure), 2)
if diastolic < 80:
if systolic < 120:
return(risk_return)
else:
risk += 0.1
return(risk_return)
return
print(blood_pressure(121,75))
when I run this code, it outputs the number as if it went though the first conditional if systolic < 120, instead of the else conditional. I need it to add the 0.1 to the original risk but its not going through properly. Sorry this is really basic I am just getting into coding. What is going wrong?
I think you calculate risk_return too early:
def blood_pressure(systolic, diastolic):
mean_pressure = (systolic + (2 * diastolic)) / 3
risk = 1.0
if diastolic < 80 and systolic >= 120:
risk += 0.1
return round((risk * mean_pressure), 2)
print(blood_pressure(121,75))

Round is returning (100, 1) instead if 100 [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 find the percentage of questions right but my percentage code is returning (100, 1) but I want it to return 100. My code is
accuracy = 100 # Setting a default.
def percentage(part, whole):
return(round((part/whole)*100),1)
and the code to print is
accuracy = percentage(questionsright, questionscompleted)
print("Your accuracy is now " + str(accuracy) + "% .")
Does anybody know why it isn't returning 100? Please Help.
You have parenthesis in the wrong places in this function:
def percentage(part, whole):
return(round((part/whole)*100),1)
Without the change, your function was returning an ordered tuple of two elements where the first element is round((part/whole)*100) and the second is 1.
Here is the corrected function:
def percentage(part, whole):
return round((part/whole)*100,1)

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

Invalid Syntax, function won't accept return instead throws invalid syntax [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 8 years ago.
Improve this question
Code below throws the error invalid syntax at the return Total line. I think there's an issue with my while loop but I don't see it. I'm trying to compute factorials with the code below. So the print factorial(4) section would compute 4 * 3 * 2 * 1 and return 24 to the console.
def factorial(x):
Total = int(x)
fact = int(x) - 1
while fact >= 1:
Total *= fact
fact -= 1\
return Total
print factorial(4)
I also tried the code below so it would print Total vs Return Total.
def factorial(x):
Total = int(x)
fact = int(x) - 1
while fact >= 1:
Total *= fact
fact -= 1\
print Total
print factorial(4)
When I use 1 as an input it returns:
Your function crashed on 1 as input because your function throws a "unsupported operand type(s) for *: 'NoneType' and 'int'" error.
Edit:
Since you're modified your question, the code you've posted is correct. The issue exists elsewhere. It's just parsing and throwing the error where you're seeing it.
Original:
It's in the line fact -= 1\. The \ tells it that the next line is actually part of the current line. so it's reading this:
fact -= 1 print Total
instead of
fact -= 1
print Total
Delete the \ at the end of this line fact -= 1\
Get rid of the \ on this line:
fact -= 1\
The \ indicates a line continuation, which would throw a syntax error
A good rule to follow: Always check the preceding line when you get a confusing syntax error

Categories

Resources