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':
Related
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 am confused between 'else' and 'else if' or 'elif' in python. I do understand that an else does not need a condition whereas an else if does need a condition as it still an if clause. Is there any other difference between these two clauses and in the way we use them? Any sort of help would be highly appreciated. Thank you!
In languages like C, there was no elif keyword; there were only nested if-else statements. (I only use C as an example of not having elif, rather than claiming that no previous or contemporary languages did not.)
/* Pretend print is a defined function */
if (x == 1) {
print("one");
} else {
/* x != 1 */
if (x == 2) {
print("two");
} else {
print("many");
}
}
Because each body only needs to be a statement, not necessarily a compound statement, the bracket in the above can be dropped.
if (x == 1)
print("one");
else
if (x == 2)
print("two");
else
print("many");
and because indentation and newlines are just arbitrary whitespace, you can rewrite this to look almost like a single statement.
/* Still two statements */
if (x == 1)
print("one");
else if (x == 2)
print("two");
else
print("many");
Other languages took the step of "merging" else and if into a single keyword to allow conditional statements with more than one condition. python and POSIX shell uses elif. (perl is an example of language with a different keyword, elsif.)
The nested statement
if x == 1:
print("one")
else:
if x == 2:
print("two")
else:
print("many")
becomes
if x == 1:
print("one")
elif x == 2:
print("two")
else:
print("many")
TL;DR
else captures every other condition not captured by if or elif. elif acts like a ladder to capture extra but totally different conditions than if. There is no else if statement in python.
Elif (else-if) - lets you check for multiple expressions.
Else - Basically means if not true then...
Example
num = random.randint(1,2)
if num == 1:
print("The number is 1")
elif num == 2:
print("The number is 2")
else:
print("The number is neither 1 or 2")
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 2 years ago.
Improve this question
word_1 = str(input(f"Enter a string: "))
word_reversed = word_1[::-1]
def check_palindrome(input_string):
if word_1 == word_reversed:
return (True)
print (f'{word_1} is a palindrome')
elif word_1 != word_reversed:
return(False)
print(f'{word_1} is not a palindrome')
print(check_palindrome(word_1))
> `Here is the output:
Enter a string: level
level is a palindrome
True`
I want to write retun true value but make it unvisible. I should run it with the return
This?
word_1 = str(input(f"Enter a string: "))
word_reversed = word_1[::-1]
def check_palindrome(input_string):
if word_1 == word_reversed:
print (f'{word_1} is a palindrome')
return (True)
elif word_1 != word_reversed:
print(f'{word_1} is not a palindrome')
return(False)
check_palindrome(word_1)
Instead of
print(check_palindrome(word_1))
You should just write
check_palindrome(word_1)
It will still print the line you expect since the function says to print it, but if you call the function in a print statement, it will always print the return value.
If you use return you assign a value to the function, printing that function first runs the function, and then prints it.
To solve this you could write : return str(word1, "is a palindrome") instead of returning True and False
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)
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")
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
try:
f = int(factor)
if (factor == 0):
print("No factors present to derive");
elif (factor != int):
print ("Numbers only please!");
elif (factor >> 4):
print("Four maximum factors!");
else:
f = int(factor);
if f == 1:
coefficientone = raw_input("What is the coefficient on the first term?")
try:
coef1 = int(coefficientone)
if (coef1 == 0):
print "No coefficient present, please re-enter polynomial"
elif (coef1 != int)
print "Numbers only please!"
else:
coef1 = int(coefficientone)
This returns a syntax error on the if f == 1: line. Please help! From what I have read on this site and most other sites that line looks to be correct. I would appreciate any help on any other portion of the code, too, for this is my first time working with python. Thanks in advance.
If you're going to add a try block, you have to have a matching except, to handle the exception you're catching.
If you don't know why you're adding the try block, it's best to simply remove it, as to not mask potential errors. It's best to only except specific error types.
Here's the fixed code. The changes are noted with comments.
try:
f = int(factor)
if (factor == 0):
print("No factors present to derive");
elif (factor != int):
print ("Numbers only please!");
elif (factor >> 4):
print("Four maximum factors!");
else:
f = int(factor);
except Exception as e: # Add this
print 'ERROR: {0}'.format(e) #
if f == 1:
coefficientone = raw_input("What is the coefficient on the first term?")
try: # Un-indent this
coef1 = int(coefficientone)
if (coef1 == 0):
print "No coefficient present, please re-enter polynomial"
elif (coef1 != int)
print "Numbers only please!"
else:
coef1 = int(coefficientone)
except Exception as e: # Add this
print 'ERROR: {0}'.format(e) #
Finally, it seems like you're planning on asking the user for as many coefficients as he specifies. For this, you'll probably want to consider using a for loop to handle the input, and a list to store the values. I'll leave the implementation details up to you. There are plenty of resources out there.
#an error that I am seeing is that factor != int should be:
elif type(factor) != int:
#and adding to the try I think that you will also need a except ValueError: