RESOURCES

COMP150: Practice Exercises

COMP150: Practice Exercises

Progression 2: 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

Create a Python script that reads three integers from the user and prints the sum of those numbers. Your program should behave like this (12, 97 and 123 are the numbers entered by the user):

    >>>
    Enter the first number: 12
    Enter the second number: 97
    Enter the third number: 123
    The sum is: ??
    >>>

num_1 = int(raw_input("Enter the first number: "))

num_2 = int(raw_input("Enter the second number: "))
num_3 = int(raw_input("Enter the third number: "))

print "The sum is: " + str(num_1 + num_2 + num_3)

Question 2

Write a Python script that asks the user for first name, last name, institution and position, and then prints the text for a name badge. Your program should behave like this (parts after the colons are entered by the user):

    >>>
    Enter first name: Thomas
    Enter last name: Anderson
    Enter institution name: MetaCortex
    Enter title: Software Developer
    Thomas Anderson
    Software Developer
    MetaCortex
    >>>

fname = raw_input("Enter first name: ")
lname = raw_input("Enter last name: ")

institution = raw_input("Enter institution name: ")
title = raw_input("Enter title: ")

print fname + ' ' + lname

print title
print institution

Question 3

Create a Python script that asks the user for a measurement in inches and prints out the measurement in centimeters. Base your calculation on 1 inch being equal to 2.54 cm. Your program should behave like this (where the value 6.5 is entered by the user).

    >>>
    Enter a measurement in inches: 6.5
    6.5 inches is equal to 16.51cm.
    >>>

num_inches = float(raw_input("Enter a measurement in inches: "))

print str(num_inches) + " inches is equal to " + str(num_inches * 2.54) + "cm."

# OR you could break it down a bit more:

num_cm = num_inches * 2.54
print str(num_inches) + " inches is equal to " + str(num_cm) + "cm."

Question 4

Write a Python script that asks the user how many pizza slices (sectors) are required from a circular pizza and prints the angle of each slice in degrees. Note: there are 360 degrees in a circle. Your program should behave like this (where the value 12 is entered by the user)

    >>>
    Enter the number of slices you need: 12
    Each slice can arc through 30.0 degrees.
    >>>

num_slices = int(raw_input("Enter the number of slices you need: "))

print "Each slice can arc through " + str(360.0/num_slices) + " degrees."

# OR you could break it down a bit more:

num_degrees = 360.0/num_slices
print "Each slice can arc through" + str(num_degrees) + " degrees."

Question 5

Write a function that takes four arguments, a, b, c, d, and prints the remainder when the sum of the first three is divided by d. For example the function might be be called like this:

func_name(3,5,7,6)

in which case the result printed out would be 3. Call the function in main with the values of your choice.

def add_three_and_divide(a, b, c, d):

    print (a + b + c)/d # Feel free to break this down a bit more if it feels more comfortable

# main routine
add_three_and_divide(3, 5, 7, 6)

Question 6

In a Python script write a function called book_sticker that takes 2 arguments (title and author) and prints out the line:

ON SPECIAL: ''title'' by ''author''! BUY NOW!

Add a main routine that prompts the user for a title and author and stores them in two variables. The main routine then calls book_sticker, passing in the user inputs.

E.g. For the user input ''Clean Code'' and ''Robert C. Martin'' it will produce the line: ON SPECIAL: Clean Code by Robert C. Martin! BUY NOW!

def book_sticker(title, author):

    print "ON SPECIAL: " + title + " by " +  author + "! BUY NOW!"

# main routine
title = raw_input("Enter title of book: ")

author = raw_input("Enter name of author: " )
book_sticker(title, author)