User Input & Functions

This lesson will teach you how to take in user input and apply it with more turtle functions.

Input Function



    We can get input from a user through the python shell to make our programs run differently each time.

    The way we do this is by storing the value returned by the input() function into a variable.

    The input() takes a string as an argument which is the prompt the user sees.

    The user can then enter anything and the program will only continue after the user presses Enter.

    import turtle
    
    window = turtle.Screen()
    pen = turtle.Turtle()
    
    window.title("User Input")
    
    #Get user input for color
    selectedColor = input("What color would you like to use? ")
    pen.color(selectedColor)
    
    pen.forward(200)
                    

    Remember to type your input in the shell window. The turtle can't move until it's given a color from the user.



    When taking in integer input, you need to specifically tell python that it is an integer and not a string. We can do this through casting.

    We can cast to an integer by writing int() and typing the thing we want to convert inside the parentheses.

    import turtle
    
    window = turtle.Screen()
    pen = turtle.Turtle()
    
    window.title("User Input")
    
    #Get user input for color
    selectedColor = input("What color would you like to use? ")
    pen.color(selectedColor)
    
    #Get user input for movement
    size = int(input("How far would you like to move? "))
    pen.forward(size)
                    

    In this example, the input is passed into int() and cast to an integer.



Challenge: User Drawing



    1. Create a new file and save it as user_input.py
    2. Write a program that draws something based on user input.
    3. Be creative!

    4. a. The input can be for properties like color, pen size, movement length, etc.

      b. Remember to cast to an integer when necessary.




Creating Functions



    Basic Functions

    Functions are used to contain code we want to execute more than once and in different places.

    We have already seen functions like print(), input() and many for turtle, but we can also create our own functions.

    We can define a new function with the keyword def followed by the name of our function and ending with parentheses () and a colon : .

    #This is a function definition 
    def print_name():
    

    We can tell a function to run (or call the function) by typing out its name just like with turtle functions or print().

    # Define function to create it
    def print_name():
        print("First Last")
    
    # Call the function to run it
    print_name()
    
    # Output:
    # First Last
                    


    Note: When you press Enter after typing the first line of the function, the next line will automatically be indented. The indents may be tabs or spaces.

    All the lines that are indented will be inside the function, if you press Backspace on an empty line, you will no longer be adding to the function.


    #These functions will work
    def print_name():
        print("First Last")
    
    def print_name():
     print("First Last")
    
    #This function will not
    def print_name():
    print("First Last")
    



    Functions with Parameters

    We can define our functions to have parameters so they can accept arguments.

    We do this by writing a placeholder name in the parentheses that acts as a variable.

    # Define function with parameter
    def print_name(name):
        print(name)
    
    # Call the function to run it
    print_name("John Doe")
    
    # Output:
    # John Doe
                    

    Functions with Return Values

    We can tell a function to return a value when it has finished running.

    E.g. if we have a function that adds two numbers together, we would want to return the result back.

    # Define function with return
    def add_numbers(num1, num2):
        result = num1 + num2
        return result
    
    # Call the function to run it
    sum = add_numbers(3, 4)
    print(sum)
    
    # Output:
    # 7
                  

Challenge: First Function



    1. Create a new file and save it as first_function.py
    2. Write a function that squares a number (multiplies the number by itself) and returns the result.

    3. a. Give the function a good name that describes what it does.

      b. The function should have one integer parameter.

      c. Test your function by calling it and printing the result.