The goal of this assignment is to allow you to practice employing
conditional execution in the Python programming language. To record
your solutions and answers, create a new Jupyter Notebook titled
Notebook 02 - Conditional Execution.ipynb
and use this notebook to
complete the following activities and answer the corresponding questions.
Make sure you label your activities appropriately. That is for Activity 1, have a header cell that is titled Activity 1. Likewise, use the Markdown cells to answer the questions (include the questions above the answer).
This Notebook assignment is due Midnight Wednesday, September 16, 2015 and is to be done individually.
For this activity, you are to read three numbers from the user using raw_input and determine which one is the maximum using conditional statements (and without using the max function).
The output for your program should be something like the following:
Number? 1
Number? 3
Number? 2
The maximum of [1, 3, 2] is 3
After you have completed the programs above, answer the following questions:
Demonstrate your program, with the following inputs:
A. 1, 2, 3
B. 1, 2, 1
C. 3, 2, 1
D. 2, 2, 2
Modify your program (i.e. copy the cell and then edit it) so that instead of printing out the maximum number, it reports whether or not the numbers are in increasing, decreasing, or random order.
The output for your program should be something like the following:
Number? 1
Number? 2
Number? 3
[1, 2, 3] is in increasing order
Demonstrate your program with the same inputs as in question 1.
For this activity, you are to write a program that converts from Imperial units (in, ft, mi) to the Metric system (mm, cm, m, km). As mundane as this may seem, failure to properly perform such unit conversions has led to spectactular and costly engineering mishaps in the past.
The output for your program should be something like the following:
Convert from? ft
Convert to? cm
Value? 10
10.0 ft is 304.8 cm
Note, your program must be able to support converting from inches (in), feet (ft), or miles (mi) to millimeters (mm), centimeters (cm), meters (m), or kilometers (m). For your output, round to one decimal place.
For full credit, make sure that you handle the cases where the user enters in invalid input such as incorrect unit types or negative values. In these situations, simply inform the user that they have entered invalid input by raising an Exception to the user:
Convert from? newtons
...
Exception: Sorry, but newtons is not a valid unit to convert from ('in', 'ft', 'mi')
One of the most tedious, but necessary components of the practice of programming is the validating user input. Malicious users can crash your program or make it misbehave by sending your code invalid or improper inputs. As a programer, you must learn to program defensively and handle situations where you get undesirable inputs.
For this class, unless specified, you do not need to handle user input validation. That said, it is a good practice to get into a habit of doing, and this activity does in fact require you to perform validation.
After you have completed the program above, answer the following questions:
Describe the control flow of your program. Did you use alternative execution, chained conditionals, or nested conditionals?
Demonstrate your program by performing the following conversions:
A. 10 ft to cm
B. 1.2 in to mm
C. 2381 mi to km
D. 7.1 ft to m
Hint: To avoid copying and pasting the cell over and over again, you can use %rerun to execute the previous cell.
For this activity, you are to write a solution to a variant of the infamous FizzBuzz problem:
Write a program that prints the numbers from
1
to100
. But for multiples of three print"Fizz"
instead of the number and for the multiples of five print"Buzz"
. For numbers which are multiples of both three and five print"FizzBuzz"
.
For this activity, the problem has an additional constraint: for multiples
of seven print "Boom"
, instead of the number and regardless of the above
rules.
To iterate or loop over the numbers from 1
to 100
, you can use the
following for
loop:
for number in range(1, 101):
# Conditional statements...
Inside the for
loop, you should add some conditional statements that
solve the FizzBuzzBoom problem as described above.
The output of your program should be something like the following:
1
2
Fizz
4
Buzz
Fizz
Boom
8
Fizz
Buzz
11
Fizz
13
Boom
FizzBuzz
...
Hint: You will need to use the % (modulo) operator.
While you are generally encouraged to use the Internet as a resource for programming assignments, for this problem, you are asked to refrain from looking up information specific to the FizzBuzz problem because the question is so popular and there are many solutions available.
Remember that any code you submit must be your own and that you must be able to explain every line of your program.
After you have completed the program above, answer the following questions:
Describe the control flow of your program. Did you use alternative execution, chained conditionals, or nested conditionals?
Why do you think this question often trips up candidates interviewing for programming positions? What was the trickiest part of this problem for you?
How many conditionals did you use in your solution to FizzBuzzBoom in Activity 3? For extra credit, implement the program such that it uses no more than 4 conditional statements.
Hint: You will need to use string concatenation.
To submit your notebook, follow the same directions for Notebook00, except store this notebook in the notebook02 folder.