The Python shell is telling me it is invalid syntax - if-statement

if guess == answer:
print("Correct!")
score + 1
elif:
print("Incorrect")
else:
except ValueError:
print("Expected integer")
That was a small section of my code. It states that elif: is invalid, specifically the :.

where is the condition for elif: place any condition to check it
if guess == answer:
print("Correct!")
score + 1
elif #condition :
print("Incorrect")
else:
except ValueError:
print("Expected integer")
REFERENCE

You're missing elif statement.
elif guess == <something>:

after elif you have to write an expression. For example:
elif guess == question:
print("Incorrect")

Related

Why is this If Elif statement always resulting in the "yes" variable

Basically I made this code to ask a user if they want to play ball:
print("Do you want to play ball?")
answer = input("I will throw the ball and you will catch it! Yes or no?")
if answer == 'yes' or 'Yes': {
print("Great, lets play!")
}
elif answer == 'No' or 'no': {
print("owwkayyyy...")
}
However, when it runs, if I say no it still results with the program printing "great, lets play"
I am new to coding and I seriously do not understand why this isnt working.
It is because you need fix your condition for your first if statement. It is incorrect to write:
if answer == 'yes' or 'Yes':
You need to rewrite answer like so:
if answer == "yes" or answer == "Yes":
The same thing is true for your elif statement.

Order of if statements?

I wonder if the first if statement is True, whether are the following elif executed or not (even if they are also True) ?
I have tried:
if True:
print "Hi"
elif True:
print "hello"
And it prints only "Hi". But in a more complex part of my code, I see some print outputs displayed that are within some elif statements and I just basically switch the very first if to True so that none of the elif should be executed for testing purposes. So why are the print statement that are within the elif printed?
(code is too long to print here, I just wonder if it could be an answer without...)
An elif will only be entered if the condition matches and the previous condition does not:
>>> if x == True:
... print 'hi'
... elif x == False:
... print 'bye'
...
hi
>>> if x == True:
... print 'hi'
... elif x == True:
... print 'bye' #not entered because previous condition was valid
...
hi
>>>
I'm not sure if I understand this correctly, but have you tried else?
if True:
print "Hi"
else:
print "Hello"
EDIT: Sorry, understood what you mean now..
I might be wrong, but "elif" means "else if"
So what you're basicly writing is like this:
If i have 5 apples, i put up a hand
else if i have 5 apples, i put up a leg.
using elif should however not be used at the end of a statement/code. If you aren't going to compare more than 2 statements, use 'if' and 'else'
But why should you have 2 'True'?
In an [if, elif, else] blocks only the first block with the condition that checks out will be executed.
but considering your situation I guess it needs more context to understand it fully, post part of the code you're testing or something with the same logic.

Python 2.7 - Raw_input and If and else

raw_input('What are you doing? ')
a = 'nothing'
if type(a):
print 'That seems boring'
else:
print 'Nice'
The meaning of this code, is that if a person answers the raw_input with 'nothing'. It should print that seems boring. And if a person writes something else it should print ok.
I am new to programming so please help me :)
If I understood your question correctly, what you are looking for is:
a = raw_input("What are you doing?")
Notice that the answer to the prompt is saved in the variable 'a'.
if a == 'nothing':
print 'That seems boring'
else:
print 'Nice'
Pay attention to the indentation. Also, we use '==' for comparison and '=' for assigning values to a variable.
I suggest you learn basics of python from https://www.codecademy.com/
I always like to make variables that show what they contain.
My version would look like this:
print "What are you doing?"
reply = raw_input()
if reply == "nothing":
print "That seems boring"
else:
print "Nice"
You can also do it like this, it will take input during run time in more oriented way
#!/usr/bin/env python
def user_input():
print "What are you doing?"
# takes user input
a = raw_input()
# if user enter "Nothing" or "nothing"
if (a == "Nothing") or (a == "nothing"):
print "boring"
# if he enters anything else
else:
print "nice"
if __name__ == '__main__':
user_input()

If statements and strings

How can I get my code to respond to string inputs? I want it to do something if the answer is yes or no. I tried something like this.
yes = 'yes'
yesOrNo = input('yes or no?')
if yesOrNo == yes:
print'you said yes'
No matter what I typed for the input, it always would say,'you said yes'.
In Python 2 input doesn't return a string, but an object. To do something like this you would want
choice = raw_input('yes or no?\n')
if choice == 'yes':
print 'you said yes'
else:
print 'you said no'
raw_input does return a string.

New to python - Why does only the first line print?

I'm sure this is super basic to everyone but for some reason I cannot figure out the code below only prints out "Glad to see you back at it again."
I'm new to programming and this is my first attempt to create something small to interact with. Any ideas why the other options in elif and else dont print?
def was_read():
print "Have you read this before?"
read = raw_input('Yes or No? ')
if read == 'Yes' or 'yes':
print 'Glad to see you back at it again.'
elif read == 'No' or 'no':
print 'Hope its a good one then!'
else:
print "I'm sorry I didn't understand that"
was_read()
While Python may look like English, it isn't English. What you wrote will it be interpreted like:
if (read == 'Yes') or ('yes')
'yes' is truthy, so your if statement really acts like:
if (read == 'Yes') or True
False or True and True or True are both True, so your first if statement will always be true.
Be explicit:
if read == 'Yes' or read == 'yes'
Or just do it the simpler way:
if read.lower() == 'yes'