Created
August 23, 2016 15:22
-
-
Save CrazyPython/168e95e0f7162c01ae3632f2347be56f to your computer and use it in GitHub Desktop.
valid code, part two
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# Today we will learn about if-elif-else clauses and getting input. | |
# The programming language we've been using all along is Python 3, by the way | |
# Here's the code: | |
favorite_number = int(input("What's your favorite number? ")) | |
if favorite_number > 900: | |
print("Woah, you like big numbers.") | |
elif favorite_number == 0: | |
print("Zero. The number of emptiness.") | |
elif favorite_number > 0 and favorite_number < 10: | |
print("You like digits, eh?") | |
elif favorite_number == 42: | |
print("The meaning of life!") | |
else: | |
print("You have bad taste.") | |
# and now with explanations: | |
# Here I added some extra newlines to give us more space to write comments | |
# Don't worry, blank lines don't do anything in this case. | |
favorite_number = int( # This converts the input to an integer* | |
input("What's your favorite number? ") # this gets text input from the user on the console | |
) | |
if favorite_number > 900: # In this case, the operator is ">". | |
print("Woah, you like big numbers.") # As expected, this code executes if the input is greater than 900. | |
elif favorite_number == 0: # elif is a short form of "else if". Executes if the if condition (and previous elif conditions) | |
# are all false and the current condition is true | |
# We use "==" instead of "=" for comparsion. This is less ambiguous and also tradition. | |
print("Zero. The number of emptiness.") | |
elif favorite_number > 0 and favorite_number < 10: # and is an operator that evaluates to True if both inputs are True, otherwise False | |
print("You like digits, eh?") | |
elif favorite_number == 42: # anot | |
print("The meaning of life!") | |
else: # else executes if all if and elif statements evaluate to False. | |
print("You have bad taste.") # this is just a silly insult | |
# *and if it isn't an integer, like the user enters "dogs", it'll throw an Exception (an error to you guys!) | |
# Exceptions can be handled to let the code move on, say ask the user again. But that's for another chapter! | |
# In this case, it'll throw "ValueError: invalid literal for int() with base 10: 'dog'" | |
# and by the way, us programmers are very fun and casual. If you asked any other average programmer for some | |
# sample code they'd fill it with jokes and puns too! For example, a easter egg hidden in the android platform: | |
# http://stackoverflow.com/questions/13375357/proper-use-cases-for-android-usermanager-isuseragoat |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment