Lab 4: Input

This week you learned how to take input from the user. That’s something that every program does one way or another. This lab will help you practice getting and using input.

Part 1: User Input

1.1. Use the input function to get input from the user. Print what the user types.

[ ]:

1.2. Use the input function, ask the user, “What is your name?”

[ ]:

1.3. Use the input() function to get a user’s name and print a customized greeting.

For example, if the user types “Mike Matera” the program could say:

Hello Mike Matera

[ ]:

Part 2: Input Conversions

2.1. Use input to ask the user for an integer and convert it to an int.

[ ]:

2.2. Ask the user for a number and convert it to a float.

[ ]:

2.3. Ask the user for an integer.

Print True if the integer is less than 100 and False if the integer is greater than or equal to 100.

[ ]:

2.4. Ask the user to input two numbers. Print the product of the two numbers.

[ ]:

2.5. Ask the user for an integer. Print True if the integer is even and False if the integer is odd.

Hint: an even number modulus 2 is equal to 0

[ ]:

2.6. A Madlib

Write a program that asks the user for four types of words:

  1. An exclamation

  2. A name

  3. A verb

  4. a noun

Using the input from the user and the template below, print out the completed Madlib:

template = "{exclamation}! said {name} as she {verb} away on her {noun}."
[ ]:

Part 3: User Defined Shape

Run the code in the cell below and it will create a shape() function that you can use to draw shapes.

[ ]:
from p4e.drawing import Turtle

tu = Turtle()
display(tu)

def shape(name, size=100):
    name = name.lower().strip()
    if name == 'triangle':
        polygon(3,size)
    if name == 'square':
        polygon(4,size)
    if name == 'pentagon':
        polygon(5,size)
    if name == 'hexagon':
        polygon(6,size)

def polygon(sides, size):
    tu.clear()
    for _ in range(sides):
        tu.draw(size)
        tu.turn(360/sides)

Now you can draw shapes by name:

[ ]:
shape("square")

3.1. Use the input() function to ask the user what shape to draw, then draw the shape.

[ ]:

3.2. Shape and Size

The polygon() function takes two arguments, the number of sides and the length of a side. Change the numbers in the example below until you feel comfortable with how polygon works:

[ ]:
polygon(5, 50)

Use the input() function twice to ask the user for:

  1. The number of sides they want to draw.

  2. The size of the polygon

After taking user input draw the polygon.

[ ]: