RESOURCES

COMP150: Practice Exercises

COMP150: Practice Exercises

Progression 3: Practice exercises

Make a decent attempt on your own in a python script before clicking a "Hint" button. If you do click the button, we recommend typing out the answer for yourself (as opposed to copying and pasting).

Question 1

Write a function that takes a boolean and prints it out followed by results of converting it to a string, an int and a float. Call the function in the main routine with sensible test values.

def convert_boolean(a):
    print a

    print str(a)

    print int(a)
    print float(a)

# main routine
convert_boolean(True)
convert_boolean(False)

Question 2

Write a function that takes two ints and returns True if the second int is a multiple of the first and False if it is not. Test it with sensible values in the main routine.

def check_multiple(x, y):
    if y % x == 0:
        return True

    else:
        return False

# Or more succinctly:             
def check_multiple(x, y):
    return y % x == 0

# main routine
print check_multiple(5, 20)
print check_multiple(3, 10)

Question 3

Write a function that takes 2 arguments and returns True if they are both of the same type and False if they are not.

def check_types(a, b):

    if type(a) == type(b):
        return True

    else:
        return False

           
# Or more succinctly: 

def check_types(a, b):
    return type(a) == type(b)

# main routine
print check_types("a", 1)
print check_types("a", 'a')
print check_types("False", False)
print check_types(1, 1.0)

Question 4

The movie ratings of the International Movie Database (IMDb.com) could be described as follows:

0 to less than 5: shockingly bad
5.0 - less than 7: strictly for a rainy afternoon
7.0 - less than 8: pretty good
8.0 +: a must see

In a Python script write a function that takes a single argument indicating the rating for a specific movie and returns the appropiate description.

Add a main routine that asks the user for a rating, passes it to the function and prints out the returned value in a sentence as shown (where 3.3 is the value entered by the user).

>>>
Enter the movie rating: 3.3
A rating of 3.3 indicates that this movie is shockingly bad.
>>>

Add further statements to main to test your function with a good selection of hardcoded values. You may assume that the user will enter a non-negative number.

def imdb_rating(rating):
    if rating < 5:

        return 'shockingly bad'
    elif rating < 7:
        return 'strictly for a rainy afternoon'

    elif rating < 8:
        return 'pretty good'
    else:
        return 'a must see'

# main routine
rating = float(raw_input("Enter the movie rating: "))

rating_string =  imdb_rating(rating)
print "A rating of " + str(rating) + " indicates that this movie is " + rating_string + "."

# alternatively print the result of the function call directly:
  
rating = float(raw_input("Enter the movie rating: "))
print "A rating of " + str(rating) + " indicates that this movie is " + imdb_rating(rating) + "."

# more tests:
print imdb_rating(4.99)
print imdb_rating(7.9)
print imdb_rating(8.1)

Question 5

Write a function that returns True if its argument is a valid "Monday" word and False if it is not. Valid "Monday" words include: "Monday", "Mon", "mon", "MON", "MONDAY".

Statements in the main routine should print the output of the function. Use sensible test values.

def check_monday(s):
    if s == "Monday":
        return True

  
    elif s == "mon":
        return True
    elif s == "Mon":
        return True
    elif s == "MON":
        return True
    elif s == "MONDAY":
        return True

    else:
        return False

            
# or MUCH more succinctly:  
          
def check_monday(s):
    return s == "Monday" or s == "mon" or s == "Mon" or s == "MON" or s == "MONDAY"

# main routine  
print check_monday("MON")
print check_monday("MoN") # etc