1 Pair programming - Week 2

2 Exercise 1

Create a program that asks the user for a number and identifies if the number is even or odd.

Step 1: ask the user to input the number. Hint: you can use the function input. You can read more about it here.

Code
res = input("Input a number: ")
print("You input %s"%res)
You input 12

The function input will input a string by default. We need to convert this to an integer. Check the use of the function int here).

Code
try:
    res_int = int(res)
except:
    print("Please input a number")

Step 2: check if the numer is even or odd. Hint: check the use of the modulus operator % here.

Code
modulus = res_int % 2

Step 3: complete the structure below so it print the correct solution. We have not seen conditionals yet in the course lectures, but their structure is quite straightforward: if the number is odd (its modulus will be bigger than zero), then print “You picked an odd number”. Otherwise (if the number is even), then print “You picked an even number”. Change “operator” and “number” by their correct values.

if mod <operator> <number>:
    print("You picked an odd number.")
else:
    print("You picked an even number.")
Code
if modulus == 0:
    print("%s is even"%res_int)
else:
    print("%s is odd"%res_int)
12 is even

3 Exercise 2

Create a program that asks the user to enter their name and their age. Print out a message addressed to them that tells them the year that they will turn 100 years old.

Step 1: ask the user to input their name.

Code
import re
while 1:
    username = input("Please input your name: ")
    if re.search("[^a-zA-Z ]", username):
        print("Please remove those special characters")
    else:
        break
print("Hello, %s"%username)
Please remove those special characters
Hello, Jasper

Step 2: ask the user to input their age.

Code
import dateutil.parser
import datetime
import dateutil.relativedelta
while 1:
    userdob = input("Please input your date of birth: ")
    try:
        dob_parsed = dateutil.parser.parse(userdob)
    except:
        print("Your date information could not be parsed")
        continue
    today = datetime.datetime.today()
    age = dateutil.relativedelta.relativedelta(today, dob_parsed)
    print("Your current age is: %s years, %s months, %s days"%(age.years, age.months, age.days))
    time_to_100 = (dateutil.relativedelta.relativedelta(years=+100) - age) 
    print("You will turn 100 years old in %s years, %s months, %s days"%(time_to_100.years, time_to_100.months, time_to_100.days)) 
    break
Your date information could not be parsed
Your current age is: 22 years, 8 months, 26 days
You will turn 100 years old in 78 years, -8 months, -26 days

Convert the age to an integer.

Step 3: Calculate the year they will turn 100.

Step 4: Display in screen the solution. Hint: you need to convert the integers to string using the function str in order to print them in line!

4 Exercise 3

Given two integer numbers return their product. If the product is greater than 1000, then return their sum.

Step 1: Ask the user to input the two numbers.

Code
while 1:
    res = input("Please input two numbers, separated with a comma: ")
    try:
        nums = res.split(',')
    except:
        print("Please separate the numbers with a comma")
        continue
    if len(nums) != 2:
        print("Please input two numbers only")
        continue
    try:
        num1 = int(nums[0])
        num2 = int(nums[1])
    except:
        print("Please input two *numbers*")

Convert both numbers to integers.

Step 2: Calculate the product of both numbers.

Step 3: Copy-paste the following conditional in the next cell and finalise it. The coditional is checking if the number is smaller than 1,000. If that is true, it will print the product (XXX). If that is NOT true, then it will calculate and print the sum of both numbers (YYY).

You need to fill in the correct items in lieu of XXX and YYY:

    if product <= 1000:
        print(XXX)
    else:
        # product is greater than 1000 calculate sum
        print(YYY)

5 Exercise 4

Write a program that accepts the radius of a circle from the user and compute the area.

Step 1: Ask user for the radius of the circle.

Convert the radius to float so we can operate with it (remember, otherwise it’s a string by default!)

Step 2: create the number pi. Four digits after the decimal point are enough for now.

Step 3: calculate the area

Step 4: print the result. Hint: you need to convert the integers to string using the function str in order to print them in line!