CSCI 101 - Intro to Computer Science

Python Lab

Quick Links: zyBook | Piazza | Canvas | CS @ Mines

Home | Contact | Schedule | Assignments | Syllabus |

Python Lab 5: Binary-Hex-Decimal Converter
Due Tuesday, October 5th, by 11:45pm


Introduction


To get started, open IDLE and then create a New File via the File menu. We suggest you immediately save this file in the directory you created to manage all your Python Labs this semester (e.g., CSCI101/PythonLabs). Save this file as either Lab5-converter.py or Lab5-ec-converter.py, depending on whether you do the extra credit (ec) or not.

Assignment

We recently learned that virtually all computers ever built use the binary numbering system internally. That is, inside your phone, computer, or any electrical device with transistors (tiny switches that are ON or OFF), there are billions and billions of switches (like the LightByte) that are ON or OFF.

Binary, the base-2 positional numbering system, is used for all data, including numbers, letters, graphics, images, sounds, etc.

Recall from lecture the following algorithm to convert from binary (base 2) to decimal (base 10):
  1. Input the binary number.
  2. Set a decimal number variable to zero.
  3. Starting with the least significant digit (the rightmost one), multiply the digit by the value of the position (i.e., 2^0 for rightmost bit, 2^1 for bit next to rightmost bit, 2^2 for bit next to that, etc.). Add the result to your decimal number.
  4. Continue doing the previous step until you reach the most significant digit (the leftmost one).
  5. Output the decimal equivalent of the given binary number.
Furthermore, one way to convert from binary to decimal (base 10) is using the Divide by 2 algorithm:
  1. Input the decimal number.
  2. Create an empty string.
  3. Store the remainder when the input number is divided by 2. The remainder will be one bit in your binary number. (HINT: use the python operator modulo % to get the remainder.)
  4. Concatenate the remainder value to your string.
  5. Divide the number by 2.
  6. Repeat the above three steps until the number is greater than zero.
  7. Output the string in reverse order for the binary equivalent of the given decimal number.
Additionally, we also went over the Hexadecimal (base 16) system. The conversion from Hexadecimal to Decimal and Decimal to Hexadecimal is very similar to binary. The differences are using base 16 instead of base 2 and that hexadecimal is represented alphanumerically with characters 0-F.

In previous assignments, we have built real-world applications (e.g., alarm clock and fraction calculator). We add another helpful tool to our porfolio with this lab.

This week, your task is to develop a multi-functional Binary-Decimal Converter (or Binary-Hex-Decimal Converter to earn extra credit) that accepts the user's input and then calculates the proper conversion for some operation. (If you include hexadecimal conversion you will earn up to 3 points of extra credit!) Your program should complete a single operation (i.e., an operation chosen by the user) until the user chooses to quit the program.

This program should ask the user to select one of the following five options (three options if you do NOT do the extra credit):
  1. Binary-Decimal Conversion
  2. Decimal-Binary Conversion
  3. Hex-Decimal Conversion
  4. Decimal-Hex Conversion
  5. Quit
If the user selects 5, output:
        OUTPUT Goodbye!
        =+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=
      
After finishing an operation (1-4 menu choice), the program should ask the user whether to continue. If the user says "y" or "yES" value (e.g., y, Y, yes, yES, yEs, ...), display the five options again. Otherwise, output "Goodbye!" and exit the program. (HINT: Use the built-in lower() function to simplify all the variations of y and yes.)

Other notes

In the real world, a user may enter a variety of unexpected inputs and sometimes our program can only understand certain responses. In this week's lab, we start to consider unexpected inputs.

If the user inputs an option that is not in [1-5] (or [1-3] without the extra credit), print "ERROR: Please choose from [1-5]".

In addition, when your program executes it should check to verify the number provided by the user is valid.

For example, in the binary system, every digit in the input string must be either ["0", "1"]. If not, output "ERROR: Input X is NOT a binary number.")

In the hexadecimal (base 16) system, every digit in the input string must be within ["0", "1", "2", "3", "4", "5", "6", "7", "8", "9", "A", "B", "C", "D", "E", "F"]. If not, output "ERROR: Input X is NOT a hexadecimal number."

Lastly, if the user chooses 2 or 4 from the menu and does NOT input a decimal number, then output "ERROR: Input X is NOT a decimal number." That is, your program should check each digit is within ["0", "1", "2", "3", "4", "5", "6", "7", "8", "9"].

HINT: for each number provided by the user (binary, hexadecimal, decimal), we suggest you define a list with the appropriate 'characters' allowed and then use the membership in operator to verify each 'character' in the list is a member of the allowed 'characters'.

This lab combines several of the fundamentals that we have learned so far, including strings, conditionals, branching, variables, lists, and while loops. Let's build/practice what you know!

HINT: Your code for this lab will be longer than your previous labs. We encourage you to write pseudocode for the whole lab FIRST, and then practice the incremental build approach. That is, implement (and test thoroughly) each piece of the overall assignment one at a time. For example, implement/test the menu (including the ERROR possibility), then implement/test menu choice 1, etc.

NOTE: You are NOT allowed to use the Python built-in methods such as int('11111111', 2) OR int('11111111', 16). We have learned the theoretical concepts in class to do this lab; your task is to apply the algorithms/pseudocode and practice coding!

Lab I/O Format

Throughout this semester we use a specific Lab Input/Output Format. This format is described below:
  • When prompting for input, use the prompt string WORD>, where WORD is a single, uppercase word which describes the input. For example, this lab might choose: OPTION>, BINARY-STR> (and others similar, see examples), and CONTINUE>.
  • When providing output that will be graded, start the line with OUTPUT. Think of this as "boxing your answer" on a math worksheet; it lets us quickly find your answer. Our grading script will skip any output lines that do not start with OUTPUT.
  • You are welcome (and should!) have other output lines that do not begin with OUTPUT; while our grading script will ignore these, good programmers include print statements that are informational to the user of the program.
  • A submission without exactly correct output formatting will receive an AUTOMATIC ZERO. This is because Gradescope is automated—it does not look at your code, only the results, and thus the format of the results must be consistent for all students.

Example Execution 1 (w/o extra credit)

Welcome to the Binary-Decimal Converter
=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=
Enter an option:
1. Binary-Decimal Conversion
2. Decimal-Binary Conversion
3. Quit
OPTION> 1
BINARY-STR> 0110
Binary 0110 is Decimal 6
OUTPUT 6
Would you like to continue (y/n)?
CONTINUE> YES
=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=
Enter an option:
1. Binary-Decimal Conversion
2. Decimal-Binary Conversion
3. Quit
OPTION> 2
DECIMAL-STR> 6
Decimal 6 is Binary 110
OUTPUT 110
Would you like to continue (y/n)?
CONTINUE> n
OUTPUT Goodbye!
=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=
      

Example Execution 2 (w/o extra credit)

Welcome to the Binary-Hex-Decimal Converter
=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=
Enter an option:
1. Binary-Decimal Conversion
2. Decimal-Binary Conversion
3. Quit
OPTION> 4
ERROR: Please choose from [1-3]
OUTPUT ERROR
Would you like to continue (y/n)?
CONTINUE> YES
=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=
Enter an option:
1. Binary-Decimal Conversion
2. Decimal-Binary Conversion
3. Quit
OPTION> 3
OUTPUT Goodbye!
=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=
			

Example Execution 3 (with extra credit)

Welcome to the Binary-Hex-Decimal Converter
=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=
Enter an option:
1. Binary-Decimal Conversion
2. Decimal-Binary Conversion
3. Hexadecimal-Decimal Conversion
4. Decimal-Hexadecimal Conversion
5. Quit
OPTION> 3
HEX-STR> 3A
Hexadecimal 3A is Decimal 58
OUTPUT 58
Would you like to continue (y/n)?
CONTINUE> Yes
=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=
Enter an option:
1. Binary-Decimal Conversion
2. Decimal-Binary Conversion
3. Hexadecimal-Decimal Conversion
4. Decimal-Hexadecimal Conversion
5. Quit
OPTION> 4
DECIMAL-STR> 25
Decimal 25 is Hexadecimal 19
OUTPUT 19
Would you like to continue (y/n)?
CONTINUE> NO
OUTPUT Goodbye!
=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=
          

Example Execution 4 (with extra credit)

Welcome to the Binary-Hex-Decimal Converter
=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=
Enter an option:
1. Binary-Decimal Conversion
2. Decimal-Binary Conversion
3. Hexadecimal-Decimal Conversion
4. Decimal-Hexadecimal Conversion
5. Quit
OPTION> 1
BINARY-STR> 1131
ERROR: Input 1131 is NOT a binary number.
OUTPUT ERROR
Would you like to continue (y/n)?
CONTINUE> y
=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=
Enter an option:
1. Binary-Decimal Conversion
2. Decimal-Binary Conversion
3. Hexadecimal-Decimal Conversion
4. Decimal-Hexadecimal Conversion
5. Quit
OPTION> 3
HEX-STR> 7777G
ERROR: Input 7777G is NOT a hexadecimal number.
OUTPUT ERROR
Would you like to continue (y/n)?
CONTINUE> YeS
=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=
Enter an option:
1. Binary-Decimal Conversion
2. Decimal-Binary Conversion
3. Hexadecimal-Decimal Conversion
4. Decimal-Hexadecimal Conversion
5. Quit
OPTION> 4
DECIMAL-STR> 123abc
ERROR: Input 123abc is NOT a decimal number.
OUTPUT ERROR
Would you like to continue (y/n)?
CONTINUE> NO!
OUTPUT Goodbye!
=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=

        

Gradescope Submission Nuances

When you submit your Python file to Gradescope, multiple different test cases are run on your code. Passing all of the tests results in a 100% on the autograded portion of the lab.

You are allowed to submit to Gradescope four times (or less) for this lab. Please IGNORE the automated email from Gradescope, this information is not correct. The maximum grade of your submissions will be your grade for the lab. Note: If your code doesn’t work (e.g., a syntax error exists, or an error is thrown in execution), then you will receive an AUTOMATIC ZERO. You should test your code before submitting to ensure it executes correctly.

Comments

All Python files should include a header with your name, section, assignment info, references (i.e., who did you collaborate with on this assignment?; what resource did you use?), and approximate time taken to do the assignment. Be sure to cite any allowed external references used to complete the assignment. Any code without this header will lose 1 point. Here's an example:

# YOUR NAME
#  CSCI 101 – Section F
#  Python Lab 5
#  References: TA James helped me with hexadecimal conversion
#  References: friend Tow Mater helped me to find a syntax error with the input
#  Time: 90 minutes

Submission

Once you are satisfied with your solution to this lab, you need to submit the file to Gradescope. In Gradescope, go to CSCI 101 > Lab5 and upload Lab5-converter.py (or Lab5-ec-converter.py, if you did the extra credit).

Note: this lab is worth 6 points. To receive full credit, your code must execute in Python 3, and you must submit a single file (your Python code file). You can earn an additional 3 points if you complete the extra credit.

Whenever you submit something to Gradescope, we strongly recommend you always double check what you submitted actually got submitted correctly (e.g., did the file upload correctly? did you submit the correct file? etc.) If your submission is incorrect, it's on you.



Valid HTML 4.01 Strict Valid CSS! Level Triple-A conformance, W3C WAI Web Content Accessibility Guidelines 2.0