Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
0% found this document useful (0 votes)
11 views

ICS3C Unit 2 - Programming (Module VII)

Codr

Uploaded by

Hockey for life
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
11 views

ICS3C Unit 2 - Programming (Module VII)

Codr

Uploaded by

Hockey for life
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 4

PROGRAMMING

MODULE VII

SELECTION
ICS3C Programming
Learning Goals –
Module VII to be able to write computer
programs that include a
Selection decision structure for two
choices

Part A If-Then-Else
1. Open template, then enter the following program and save as pr7_1:

Description: Practice with If-Then-Else statements.


Input: name and age
Output: a message

cls()

# input a name and age


print("Enter your name: ", end="")
name = input()
print("How old are you? ", end="")
age = int(input())

# determine the type of message to be displayed


print()
if age >= 18:
print("You are an adult now, ", name)
else:
print("You are still a child, ", name)

print()
print("Thank you for using my program.")

This program uses a simple if... else... structure to print one of two possible
messages depending on whether the variable age is at least 18 or not. If the condition
age >= 18 is true then the first message is printed. If it is false then the second
message is printed.

Run your program. For the age input:

What happens when you enter 17? _________________________________

What happens when you enter 18? _________________________________

What happens when you enter 19? _________________________________

_____________________________________________________________________________________
Page 7-2
ICS3C Programming

2. Markham Parks and Rec is planning a ski day for young children. Town employees must
pay $42 per child. Other Markham residents must pay $50 per child. Write a program
that asks the user if he/she is a town employee and then asks the user how many
children he/she wishes to sign up for the ski day. Your program should then tell the user
the cost per child and the total cost. Save as pr7_2. Use only one if statement.

Part B Random Numbers


There is a command in Python that will generate a random number.

randint() is a built in function in Python that returns a random integer.

The command number = randint(0, 3) will assign a random number from 0 to 3 to number

You can use this function to come up with many different random numbers, e.g.:

number = randint(11, 20) will assign a random number from 11 to 20 to number

1. Enter the following program and run it. Save as pr7_3.

cls()
number1 = randint(0, 9)
number2 = randint(0, 9)

print("What is ", number1, " + ", number2, " ?: ", end="")


sum = int(input())

Run this program many times.

What does randint(0, 9) do? _____________________________________

What would randint(0, 4) do? _____________________________________

Add statements that will tell the user if their answer is correct or not. If the answer is not
correct, tell the user the correct answer. Use only one if.

2. Create a program for primary students to practice subtraction. (Remember that they do
not know about negative numbers.) Your program will generate 2 integers from 1 to 10
and then give the user a subtraction question with these numbers. Tell the user if their
answer is correct or incorrect. If the answer is correct, draw a happy face on the screen.
If the answer is not correct, tell the user the correct answer. Save as pr7_4.

_____________________________________________________________________________________
Page 7-3
ICS3C Programming

Part C Efficiency
Suppose you were asked to write a program asking a person if they had a two-door car or a
four-door car. Then you were to draw a picture of a car with the appropriate number of doors,
depending on what they entered. If they entered a two-door car then the picture would be of a
car with one door. If they entered a four-door car, the picture would be of a car with two doors.

Here is a sample Algorithm of the wrong (inefficient) way to program this:

ask user how many doors on their car


get and store number of doors

if the number of doors is two:


draw a picture of a car with one door (i.e. body, wheels, roof, lights etc... and one door)
if the number of doors is four:
draw a picture of a car with two doors (i.e. body, wheels, roof, lights etc...and two doors)

To draw a picture of a car, you would need to write the code for the wheels, the body, the roof
and windshields, and the lights. You may even need to code a road for the car to sit on. Why
would you put all this code twice?

Here is a sample Algorithm of the efficient way to program this:

ask user how many doors on their car


get and store the number of doors

draw the body, wheels, roof, lights etc. of the car

If the number of doors is two:


draw one door on the car body
If the number of doors is four:
draw two doors on the car body

1. Write a program that asks a user how many windows there are on the second floor of
their house. Give them a choice of two or three windows. After they have entered their
response, have a picture drawn that has the appropriate number of windows appear on
the second floor. Be efficient with your code!! Save as pr7_5.

_____________________________________________________________________________________
Page 7-4

You might also like