Exercises: Conditionals

posted by: Ms. Martin 3 March 2010 One Comment

Make sure you have read and understood conditional notes before you try these.

As usual, feel free to skip around. You need to turn in at least 4 of these!

Activity 1: Speeding

You are driving a little too fast, and a police officer stops you. Write a function that given your speed, tells you what kind of ticket you get. If speed is 60 or less, print “no ticket.” If the speed is between 61 and 80 inclusive, print “small ticket.” If the speed is 81 or more, print “big ticket.”

Activity 2: Sum triple

Write a function that takes two integers as parameters. If the two integers are equal, you should print out triple the value. Otherwise, you should print out the sum of the two numbers.

Try these examples (the first should print 7, the second 12 and the last 32):

sumTriple(3,4)
sumTriple(4,4)
sumTriple(9,23)

Activity 3: Absolute value

Write a function absolute_value that prompts the user for a number. Your function should print the absolute value of that number. In other words, if the number is positive, just print the number. Otherwise, negate it.

Here are two sample runs:

Integer? -34
The absolute value of -34 is 34

Integer? 1
The absolute value of 1 is 1

Activity 4: Validate triangle

Write a function validate_triangle that takes three parameters representing the three sides of a triangle. Your function should print out “right triangle” if the sides can be the sides of a right triangle or “not right triangle” otherwise. A right triangle’s sides obey the Pythagorean theorem. Keep in mind that you don’t know what order the parameters will be given in!

Try these examples (the first three should print “right triangle” and the last should print “not right triangle”):

validate_triangle(3,4,5)
validate_triangle(5,3,4)
validate_triangle(3,5,4)
validate_triangle(4,5,6)

Activity 5: Days in

Write a function called days_in that prompts the user for the number of a month (1 for January, 2 for February, etc) and prints out the number of days in that month. You may find this rhyme useful:

30 days hath September, April, June and November,
All the rest have 31, Excepting February alone
(And that has 28 days clear, With 29 in each leap year).

You can ignore leap year!

Activity 6: Guess my number

Play a game with the user! Give him/her five chances to guess a secret number you will have stored in a variable. If the number the user tries is smaller than your secret number, print “too low,” if it’s too high print “too high” and otherwise print “just right.” Your game should always have the user guess 5 times, even if they get it right the first time!

To do this, you will need to prompt the user in a for loop. See the following example:

for i in range(10):
    food = raw_input("Your favorite food? ")
    print("I like " + food + ", too!!")

Modulus

The next activities will require you to use the modulus operator or mod. In Python, it is represented by the percent sign — % — and is used to get the remainder from integer division. For example, 5 % 2 is 1 because 2 goes into 5 two times with a remainder of 1. 10 % 7 is 3 because 7 goes into 10 once with a remainder of 3. Try some expressions with % until you understand how it works.

Next, see how mod can be useful with conditionals:

#Asks for a number.
#Prints if it is even or odd

number = raw_input("Tell me a number: ")
number = float(number)
if(number % 2 == 0):
    print (number,"is even.")
elif(number % 2 == 1):
    print (number,"is odd.")
else:
    print (number,"is very strange.")

We can use the mod operator combined with the idea of a changing i in a for loop to create some interesting results, such as this shape:

Using mod makes this very easy!

for i in range(12):
    if(i % 3 == 0):
        color("red")
    elif(i % 3 == 1):
        color("purple")
    else:
        color("green")
    circle(i*5)
hideturtle()

Activity 7: Colorful stack

Use this idea to create a colorful stack such as the following:

Can you make your other shapes (growing stars, for example) more interesting using this idea?

Activity 8: Factors

Use the mod idea to write a function that prints all factors of a number. Make sure it works for negatives, too.

To do this, you will need to start your loop couter i at 1 rather than 0. Use for i in range(1, n).

Activity 9: Leap year

Write a function called is_leap_year that prompts the user for a year and prints out whether it is a leap year. You will find this algorithm useful:
A year is a leap year if it is divisible by 4 but not by 100 OR it is divisible by 400

1 Star2 Stars3 Stars4 Stars5 Stars (8 votes, average: 2.88 out of 5)
Loading ... Loading ...

Leave a comment

XHTML: You can use these tags: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>