CMSC 435: Assignment 0
Overview
Write a Python 3 program that inputs three weights and heights and
outputs three body mass indices (BMI-s), one corresponding to each
(weight, height) input pair. Process the three input pairs with a loop.
Compute BMI with the following formula: \((weight * 703) /
height^2\), where height is in inches. Python has an
exponentiation operator, which you MUST use. Print calculated BMIs to
two decimal places. Use the CPython implementation of the Python
language (it is the one installed on the Linux machines).
Include a function named bmi to compute BMI given a
weight in pounds and height in inches. Your function should return the
computed BMI.
Include a comment block at the top of the program and other comments in your code.
After you finish printing the three BMIs, print the bytecode
corresponding to the bmi function (and ONLY this function).
Print the code as a string, and also as a sequence of integers in square
brackets. Next, use the dis module to disassemble the
bytecode to display instructions and operands in a more readable
format.
Lastly, answer the questions below.
Input Specification
Input three weights and three heights. Weight will be in pounds and height will be in feet and inches. All inputs will be integers. Feet and inches will be typed on the same line.
Use PRECISELY the format below with the EXACT same SPACING and SPELLING.
Weight ==> <UserInput>
Height ==> <UserInput> <UserInput>
Output Specification
Output the BMI using a field width of five with two decimal places.
Use PRECISELY the format below with the EXACT same SPACING and SPELLING.
Sample Session (ellipses show truncations)
Weight ==> 200
Height ==> 6 4
BMI = 24.34
Weight ==> 180
Height ==> 6 0
BMI = 24.41
Weight ==> 190
Height ==> 6 1
BMI = 25.06
******************
Bytecode for "bmi"
******************
b'|\x00d\x01\x14\x00...
[124, 0, 100, 1, 20, 0, ...
*********************
Disassembly for "bmi"
*********************
6 0 LOAD_FAST 0 (weight)
...
Required Types, Concepts, and Functions
main ()
# Take `weight` in pounds, and `height` in inches.
bmi (weight, height)Questions
Answer these questions in a PLAIN TEXT file named
README.txt. Include the QUESTIONS in the file as well. Use
whitespace for readability, and keep lines to AT MOST 80 characters.
Discuss how the bytecode corresponds to the disassembled code, AND how the disassembled code corresponds to the Python source code (i.e., how can I easily tell which bytecode corresponds to which Python source statement).
Discuss AT LEAST TWO benefits of CPython’s implementation, which compiles Python to bytecode, and then interprets the bytecode.
There are several types of bytecode instructions, like “General instructions”, “Binary operations”, and “Miscellaneous opcodes” (see the dis module documentation. Discuss how a specific instruction of one of the types above is executed. Limit your instruction choice from the set of instructions generated by the
dismodule. Specifically cite relevant code from ceval.c OR a file it includes.
What to Submit
Submit your Python code Bmi.py and
README.txt. Use the submit utility to submit
your code.
Gary M. Zoppetti, Ph.D.