Python elif else syntaxError [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 6 years ago.
Improve this question
whenever i try to use else and elif i get that error .
x=4
if x>0:
print("positive")
elif x=4:
print("equal")
else:
print("negative")
Msg Error :
File " '<'stdin'>' ", line 1
elif x=4 :
^
SyntaxError: invalid syntax
File "'<'stdin'>'", line 1
else:
^
SyntaxError: invalid syntax

= is an assignment operator.
== is a comparison operator.
You need to use the comparison operator in the elif statement like so:
x = 4
if x > 0:
print("positive")
elif x == 4:
print("equal")
else:
print("negative")

That is because you are using assignment operator = instead of equality operator ==.
x=4
if x>0:
print("positive")
elif x==4:
print("equal")
else:
print("negative")

Related

It shows the error as 'fact' not defined.why? [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 factorial(n):
if(n == 1):
fact = 1
return fact
else:
fact = a * factorial(n - 1)
return fact
a = int(input("Enter a number to find its factorial: ")
if (a > 0):
print("The factorial of", a, "is:", fact)
else:
print("Enter a positive value to find its factorial")
In the above code it tells me that - NameError name 'fact' is not defined .
Lines starting from a = int... should be outside your function. Once that is done all you need is to add fact = factorial(a).
Find the correct logic below.
def recur_factorial(n):
if n == 1:
return n
else:
return n*recur_factorial(n-1)
num = 7
# check if the number is negative
if num < 0:
print("Sorry, factorial does not exist for negative numbers")
elif num == 0:
print("The factorial of 0 is 1")
else:
print("The factorial of", num, "is", recur_factorial(num))

If/Elif Python Statement [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
New to this. My autochecker says that line 3 is incorrect.
score = input("Enter Score: ")
s = float(score)
if s >= 0.9
("A")
elif s >= 0.8
print ("B")
elif s >=0.7
print ("C")
elif s >=0.6
print ("D")
elif s < 0.5
print ("F")
else
print ("Enter number")
You need a colon after each if/elif/else line.
You need to add print before "A".

How to fix a while syntax error 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 4 years ago.
Improve this question
I'm getting a syntax error on my 2nd while loop. Can't figure out why, any help appreciated :)
#intro
print("Welcome to my prime number detector.")
print("Provide an integer and I will determine if it is prime.")
#again loop
again = "Y"
while again == "Y":
num = (int(input("Enter an integer"))
#check for valid input
while num < 1:
num = (int(input("Enter an integer"))
#test for prime
for d in range(2,num):
if (num % d) == 0:
print(num,"is not prime.")
else:
print(num,"is prime.")
#ask again
again = intput("Do you want to play again? (Y/N)")
You are missing a closing parenthesis ) in the two of your following lines. The correct line of code is
num = (int(input("Enter an integer")))
Also, as sheepez mentioned below, your outer brackets are redundant. You can simply use
num = int(input("Enter an integer"))

Issue with if-else statements [closed]

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Closed 6 years ago.
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.
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.
Improve this question
This is what I've input:
def greater_less_equal_5(answer):
if 6 > 5:
return 1
elif 4 < 5:
return -1
else:
return 0
print greater_less_equal_5(4)
print greater_less_equal_5(5)
print greater_less_equal_5(6)
and gave me this note
Oops, try again. It looks like your function output 1 instead of -1
when answer is 3. Make sure you filled in the if and elif statements
correctly!
and this is what came up in the upper right display:
>1
>1
>1
>None
No matter how I change around the numbers and the >/< I've even tried == and != it still outputs 1 1 1 None.
I've searched around for any possible tips and seen others stuck on the same problem as me and when I tried their solves I then get:
def greater_less_equal_5(answer):
if > 5:
return 1
elif < 5:
return -1
else:
return 0
print greater_less_equal_5(4)
print greater_less_equal_5(5)
print greater_less_equal_5(6)
and the output is:
File "python", line 2
if > 5:
^
SyntaxError: invalid syntax
Is this test rigged to always output a failed result to make me pay for pro and ask for their help?
And the hint given for this is:
Make sure the if and elif statements end with colons :
Your code should look something like:
if EXPRESSION:
do something
elif OTHER EXPRESSION:
do something
else:
do something
Am I just missing something horribly basic?
You are indeed missing something basic - namely, that the output of your function doesn't depend on answer at all. No matter what you feed in as answer, because 6 > 5 is always True, it will always return the result of that case.
What you need is
def greater_less_equal_5(answer):
if answer > 5:
return 1
elif answer < 5:
return -1
elif answer == 5:
return 0
You're missing 'answer' variable for expression, which you pass into your function
def greater_less_equal_5(answer):
if answer > 5:
return 1
elif answer < 5:
return -1
else:
return 0
print greater_less_equal_5(4)
print greater_less_equal_5(5)
print greater_less_equal_5(6)

I don't know what is wrong with my syntax [closed]

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
This question does not appear to be about programming within the scope defined in the help center.
Closed 9 years ago.
Improve this question
Here's my code;
def save(name):
if x['fname'] == 'ply.json':
save1(name)
elif x['fname'] not 'ply.json':
write_data({'fname':'ply.json', 'name':'Karatepig'}, 'ply.json')
I get an error stating that I have this syntax error:
File "<stdin>", line 4
elif x['fname'] not 'ply.json':
^
What am I doing wrong?
something not something is not a valid expression. If you want to test if it is not equal, use !=:
elif x['fname'] != 'ply.json':
However, since this is the exact opposite of the preceding if test, just use else here:
if x['fname'] == 'ply.json':
save1(name)
else:
write_data({'fname':'ply.json', 'name':'Karatepig'}, 'ply.json')
You need to use != to test inequality, like this:
elif x['fname'] != 'ply.json':
But why use elif?
def save(name):
if x['fname'] == 'ply.json':
save1(name)
else:
write_data({'fname':'ply.json', 'name':'Karatepig'}, 'ply.json)
You need to use != for "not equal":
elif x['fname'] != 'ply.json':

Categories