You don't have to know how to farm to be a chef - they work at different levels of abstraction in food production.
In computer science, you build bigger, more complex things by using the building blocks others (or you) have provided.
chef image credit: https://www.flickr.com/photos/nestle/3885866873
print("Hello world!")
type(2.70)
fahrenheit_temp = input("Enter the temperature in Fahrenheit ") )
len(name)
user_input = int( input("Enter a number greater than "+str(target_number)+" ") )
max(rainfall_amounts)
with open("gettysburg.txt") as gettysburg_file:
gettysburg_text = gettysburg_file.readlines()
Why are these awesome?
The syntax for defining your own function includes the following
def (short for define)( ) (later, we'll put things inside these parentheses):import random
def magic_8_ball():
rand_val = random.randint(1,3)
if rand_val == 1:
print("It is Certain.")
elif rand_val == 2:
print("Don't count on it.")
else:
print("Reply hazy, try again.")
This function won't run or do anything until you call it. Call it just like any other function you've used.
magic_8_ball()
Put just the magic_8_ball() function definition in a file and run it.
Notice that nothing happens. Discuss: why it does that?
Then, add a function call to your program and run it again. Discuss: what happens now, and why does it do that?
You can also now call the function interactively from the interactive shell. Try this and discuss the results.
When you test a single function in this way, you are performing a kind of unit test - this would be an important step in including the function in a larger program.
Now you can use the function you created in something bigger. This example presents the menu for the user to use the Magic 8 Ball, but we don't have to worry about how it works.
user_response = input("Do you have a question in your mind for the Magic 8 Ball? (yes/no) ")
while user_response == "yes":
print("The Magic 8 Ball says....")
magic_8_ball()
user_response = input("Do you have another question in your mind for the Magic 8 Ball? (yes/no) ")
Here's what the entire file looks like:
import random
def magic_8_ball():
rand_val = random.randint(1,3)
if rand_val == 1:
print("It is Certain.")
elif rand_val == 2:
print("Don't count on it.")
else:
print("Reply hazy, try again.")
user_response = input("Do you have a question in your mind for the Magic 8 Ball? (yes/no) ")
while user_response == "yes":
print("The Magic 8 Ball says....")
magic_8_ball()
user_response = input("Do you have another question in your mind for the Magic 8 Ball? (yes/no) ")
When you call a function, Python will remember where you left off and continue executing there when the function is complete.
import random
def magic_8_ball():
rand_val = random.randint(1,3)
if rand_val == 1:
print("It is Certain.")
elif rand_val == 2:
print("Don't count on it.")
else:
print("Reply hazy, try again.")
def magic_8_ball_dialog():
user_response = input("Do you have a question in your mind for the Magic 8 Ball? (yes/no) ")
while user_response == "yes":
print("The Magic 8 Ball says....")
magic_8_ball()
user_response = input("Do you have another question in your mind for the Magic 8 Ball? (yes/no) ")
def jack_in_the_box():
wait_time = random.randint(1,100)
counter = 0
while counter < wait_time:
print(".")
counter += 1
print("POP!")
def rubiks_cube():
#pretend there is some awesome Rubik's cube code here
print()
def main():
toy_number = 99
while toy_number != 0:
print("Available toys:")
print("1: Magic 8 Ball")
print("2: Jack-in-the-Box")
print("3: Rubik's Cube")
print("0: Quit")
toy_number = int(input("Which toy do you want to play with? "))
if toy_number == 1:
magic_8_ball_dialog()
elif toy_number == 2:
jack_in_the_box()
elif toy_number == 3:
rubiks_cube()
print("Have a nice day!")
main()