Order of if statements? - python-2.7

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.

Related

How to fix a loop with boolean variable

I'm doing a project for a class, and I opted to make a text based game in python. I'm trying to set it up so that the question will loop until the player confirms their choice, and I'm having problems with the while loop in this section.
def pc_cls_sc(x):
# code does some stuff
print "You are sure about" + str(x)
exVar = raw_input("Right?")
if exVar == "y":
print "Alright!"
conf_Class = True
else:
print "Ok then."
conf_Class = False
while conf_Class is False:
pc_Class = raw_input(#asks some question)
pc_cls_sc(pc_Class)
The rest of this code functions properly, but the loop continues after the conf_Class variable is supposed to be set to true. I have a similar loop earlier in my code, which works just fine. I've tried moving the variable reassignment outside of the pc_cls_sc function, but all it did was cause double output. Can anyone tell me how to fix this?
You can use break to exit the loop. The code below will keep asking a user for input until they say 'y'.
while True:
x=input("Right? " )
if (x=='y'):
break

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

The Python shell is telling me it is invalid syntax

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

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'

Is using If, ElseIf, ElseIf better than using If, If, If?

Is there really any difference between using
If(this)
{
}
Else If(that)
{
}
Else
{
}
or using,
If(this)
{
}
If(that)
{
}
Else
{
}
?
Does one execute any faster? Does the compiler or architecture make any difference?
There's the huge difference that the contents of the this-block and the that-block can both be executed in the second form, whereas the first form allows at most one of those to be executed.
Compare these two Python snippets:
x = 10
if x > 5:
print "x larger than 5"
elif x > 1:
print "x larger than 1"
else:
print "x not larger than 1"
# output: x larger than 5
and
x = 10
if x > 5:
print "x larger than 5"
if x > 1: # not using else-if anymore!
print "x larger than 1"
else:
print "x not larger than 1"
# output line 1: x larger than 5
# output line 2: x larger than 1
As others have mentioned, you generally shouldn't be concerned about performance between these variations so much as you should be concerned about correctness. However, since you asked... all else being equal, the second form will be slower because the second conditional must be evaluated.
But unless you have determined that code written in this form is a bottleneck, it's not really worth your effort to even think about optimizing it. In switching from the first form to the second, you give up the jump out of the first if-statement and get back a forced evaluation of the second condition. Depending on the language, that's probably an extremely negligible difference.
Yes, in your first example, if this evaluates to true and that evaluates to true, only the first code block will be executed, whereas in the second example, they both will.
They are not equivalent
Yes.
In the First case: control-flow will only check the next condition if the current condition fails but in the second case it will check all conditions that come across.
In first case Else part will only be executed if all previous conditions fails to be true. and in the second case only if the last If condition fails.
it makes a difference.
in case "this" and "that" are both true, both part of code will result something else.
In first code.
it will check IF, if this is true then execute its body,
if false then will
check ELSEIF if that is true that execute its body,
if false then will
execute body of else.
Second code
it will check IF, if this is true then execute its body,
if false do nothing.
check 2nd IF if that is true that execute its body,
if false then will
execute body of else.