In JavaScript, one could do this:
if (integer > 3 && integer < 34){ document.write("Something") }
Is this possible in Python?
It works that way in real life, and it works that way in Python. if statements can be nested within other if statements. This can actually be done indefinitely, and it doesn't matter where they are nested. You could put a second if within the initial if .
Assign a bool value to a variable called playing, and then use that as the loop condition. So you would have; playing = True while playing: choice = input("would you like to play again? y/n: ") if choice == "n": print "Thanks for playing" playing = False else: print "play again.. etc..."
Python indeed allows you to do such a thing
if integer > 3 and integer < 34
Python is also smart enough to handle:
if 3 < integer < 34: # do your stuff
Python replaces the usual C-style boolean operators (&&
, ||
, !
) with words: and
, or
, and not
respectively.
So you can do things like:
if (isLarge and isHappy) or (isSmall and not isBlue):
which makes things more readable.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With