0% found this document useful (0 votes)
134 views

Python: Lab 4 Conditionals

The document describes a Python program to practice using if statements to calculate speeding ticket fines. The program asks users to input a speed limit and their driving speed, calculates how much they were speeding by, and uses if/else statements to print the appropriate fine based on the speed exceeded: $100 if less than 15 mph over, $200 if 15-30 mph over, or $500 plus a mandatory court date if more than 30 mph over. The document also provides examples of using the turtle graphics module to draw shapes like lines and squares by controlling a turtle object.

Uploaded by

Abiy Alemu
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
134 views

Python: Lab 4 Conditionals

The document describes a Python program to practice using if statements to calculate speeding ticket fines. The program asks users to input a speed limit and their driving speed, calculates how much they were speeding by, and uses if/else statements to print the appropriate fine based on the speed exceeded: $100 if less than 15 mph over, $200 if 15-30 mph over, or $500 plus a mandatory court date if more than 30 mph over. The document also provides examples of using the turtle graphics module to draw shapes like lines and squares by controlling a turtle object.

Uploaded by

Abiy Alemu
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 6

Lab 4 Conditionals

Python
2. Speeding Tickets
To practice using if statements, we are going to make a program to help a police officer
calculate the appropriate fine for driving over the speed limit. The cost of the fine will depend
on how much the user was speeding by. Ask the user to input the speed limit and store it to a
variable. (Don't forget to cast it to an int!) Also ask the user to input the driving speed, storing it
in a variable as well. You’ll then need to calculate by how much the driver was speeding, and
print that to the screen for the user. Using if statements, check the amount the driver was
speeding by, and print these sentences to the screen:

If they were speeding by less than 5 MPH (including not speeding),


print “You receive no ticket… this time.”
If they were speeding by 5 to 15 MPH,
print “You receive a ticket for a $100 fine.”
If speeding by more than 15 MPH, but less than 30 MPH,
print “You receive a ticket for a $200 fine.”
Otherwise (if they were over by 30 MPH or more),

print“ You receive a ticket for a $500 fine, and mandatory court date.”
With the input given below, the output should look something like this:

Exercises
1. Write a program[lab03e01] that accepts an integer from users and prints if the entered
number is divisible by 6
2. Write a python program [lab03e02.py] that asks a user to enter an integer and tells if
the entered number is negative, zero or positive.
3. Write a python program [lab03e03.py] that asks a user to enter two integers and tells
whether the first is a multiple of the second or not.
4. Write a python program [lab03e04.py] that asks a user to enter three integers and
prints the largest among them.
5. Write a python program [lab03e05.py] that asks a user to enter score out of 100 and
prints the corresponding letter grade based on the scale given below.
Score >=85 ==> A
Score >=70 but <85 ==> B
Score >=50 but <70 ==> C
Score >=40 but <50 ==> D
Score < 40 ===> F
6. Write a program [lab03e06.py] that accepts the number of hours worked by an
employee in a week and the pay rate and computes an employee’s weekly salary based
on the following rules.
• The normal work hours for a week are 40 hours.
• The employee is paid overtime if he worked more than that.
• The pay rate is multiplied by a rate factor of 1.5 for overtime
Case Study: Turtle Graphics
There are many modules in Python that provide very powerful features that we can use in our
own programs. Some of these can send email, or fetch web pages. The one we’ll look at in this
lab allows us to create turtles and get them to draw shapes and patterns.
Turtle graphics is a method of programming “vector” graphics using a relative cursor upon a
Cartesian plane. Python has a built-in module that supports turtle graphics called the “turtle”
module. Importing this module gives you access to all the turtle graphics functions you will need
to draw vector graphics on the screen.
Example 1: Draw a line
One of the simplest things you can do using the turtle module is to draw a line. There are always
four steps you need to do in order to use the turtle module:

1. Import the turtle module. If we skip this step, there’ll be no turtle to control.
2. Create a turtle to control.
3. Draw things. Do stuff. This will also automatically create the screen.
4. Run turtle.done()

Notice that turtle.done() will pause the program. You’ll need to close the window in order to
continue.
Example 2: Drawing a square.
Lines are boring. We can rotate the turtle in order to draw more interesting figures.

1. Write a program that draws the following squares with size 100

Clues:
silly.penup() ## the turtle moves but stops drawing on the screen. Think of it as if the drawing pen is
## moved without touching the canvas
silly.pendown() ## Brings back the pen to canvas. The turtle starts plotting the path while it moves

2. Write a function that draws a square using input parameters


length: length of the side of the square
def draw_square(side_length):
# your code

3. Modify the function you wrote in question number 2 to accept the color of the square too
and draws the square with the specified color
[Clue: for color use <turtle_name>.color(color) eg: bob.color(‘red’) ]
def draw_square(side_length,color):
# your code

You might also like