0% found this document useful (0 votes)
21 views

Lab 2

Uploaded by

amnamehmoodbwp
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
21 views

Lab 2

Uploaded by

amnamehmoodbwp
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 5

Lab 2: Input, Processing and Output

For this lab, we are going to do a few small exercises to practice prompting the user for input
and using that information to perform a task. For the lab assignments, there are going to be
multiple parts. All the parts will be saved in the same .py file called lab2yourlastname.py (For
example: lab2Davison.py). Once you are finished with all the parts, submit the file to Canvas as
an attachment.

The goal of this lab is to get comfortable with Chapter 1 and 2’s lectures and materials to
practice using IDLE. We are also going to practice writing our algorithms and using
mathematical operators in Python.

Guidelines: Every program1 should start with a header. The header is a comment section that
includes your name, date, course, and purpose of the program.
If your program doesn’t have the header, then you will lose 10% of your grade.

Use the # sign to put your comments – comments won’t be run by Python, and they are only
additional information about yourself and your program.
Example:
# ========================================
# Name: Andrew Davison
# Date: 2/1/2024
# Course: CIS-115-N02
# Purpose: The goal of this program is …
# ========================================

# Problem 1: Any comments you want to put here about Part 1


Code for problem 1 goes here …

# Problem 2: Any comments you want to put here about Part 2


Code for problem 2 goes here …

# Problem 3: Any comments you want to put here about Part 3


Code for problem 3 goes here …

# Problem 4: Any comments you want to put here about Part 4


Code for problem 4 goes here …

__________________________
1 This rule applies to all the programs that you write during the semester.

Problem 1: Write a simple program that can count the powers of 2 used for the first 8 bits (also
called a byte). I would like for you to print out each power on a different line.
Example: 2**0 and 2**1 and so on.
Hint: you must use the exponentiation operator and print function for part 1.

Sample Output:
1
2
4
8
16
32
64
128

Problem 2: Write a Python program that asks the user to enter an integer value and displays
the value of 2 raised to that integer.
Hint: You need to us input, print, and int functions for part 2. You might need the str function
as well depending on how you solve the problem.

Sample Output:
What power of two? 10
Two to the power of 10 is 1024

Problem 3: Build on part 2 so that you can now ask the user for a specific integer base and
integer exponent. Then display the base raised to that exponent. Your program should function
as shown below.
Hint: You need to us input, print, and int functions for part 2. You might need the str function
as well depending on how you solve the problem.

Sample Output:
What base? 10
What power of 10? 4
10 to the power of 4 is 10000

Problem 4: Now we would like to convert a binary number into its value in base 10. This
program should allow the user to enter a four-digit binary number and then display that binary
number in base 10. Each binary digit should be entered one per line, starting with the leftmost
digit. You can find more information about binary to base 10 conversions on the slides for
Chapter 1.
Hint: you need to use multiple input functions to get the digits (0s and 1s) from the user. Then
you need to perform some calculation and then print the result.

Sample Output:
Enter the leftmost digit: 1
Enter the next digit: 0
Enter the next digit: 0
Enter the next digit: 1
The value is 9

Problem 5: We are now going to go in the other direction, and we are going to convert an
integer into binary. To do this assignment we need to introduce another mathematical operator
called the modulus operator (%). The modulus operator is used for modular division, which is
where you get the remainder of integer division. For example, 27/16 = 1, remainder of 11, so
27%16 = 11, (which is said 27 mod 16).

If we look back at the example from the slides for Chapter 1, you will notice that when we
convert base 10 to binary, we divide the number by two and the remainder makes up part of
the binary number. Here is the image to help explain:

So, hopefully after looking at this image, you can see how the modulus operator is going to
make this conversion easier. For this part of the assignment, I will give you the algorithm.
For this stage of the class, we are going to pick the number that we are converting, not the
user. We do not have access to loops yet, so we need to know exactly how many times to run
the algorithm.
Hint: follow the below algorithm exactly:
1. Pick a number between 0 and 31.
2. Create a variable to hold this number and store the number you have chosen in this
variable.
3. Use a print statement to print out to the screen to let the user know what number you
have chosen.
The number I chose was 28
4. Now print out the message
28 in binary, with the digits in reverse order is:
5. Create a variable called remainder and assign the result of (number % 2) to that
variable.
a. For example: remainder = number % 2
6. Now assign the result of number // 2 to the variable number
a. For example: number = number // 2
b. It may look a little off that the same variable name is on both sides of the = sign,
but the division on the right side of the = sign will happen first, and the translator
is “smart” enough to handle this.
7. Print the value of the remainder.
8. Repeat steps 5, 6, and 7 four more times.
a. This is done four more times because the binary number is going to contain 5
digits.

Sample Output:
The number I chose was 28
28 in binary, with the digits in reverse order is:
0
0
1
1
1

*All values for the following problems should be displayed with commas where appropriate.
Thoroughly test your code with very small and big numbers.

Problem 6: Write the Python code that prompts the user for two integer values and displays
the result of the first number divided by the second, with exactly three decimal places
displayed.

Problem 7: Write the Python code that prompts the user for two floating-point values and
displays the result of the first number divided by the second, with exactly five decimal places
displayed.
Problem 8: Write the Python code that prompts the user for two floating-point values and
displays the result of the first number divided by the second, with exactly six decimal places
displayed in scientific notation.

Problem 9: Write the Python code that allows the user to enter two integer values and displays
the results when each of the following arithmetic operators are applied.

For example, if the user enters the values 7 and 5, the output would be.

7+5=12
7-5=2
7*5=35
7/5=1.40
7//5=1
7%5=2
7**5=16,807

Another example, if the user enters the values 4 and 2, the output would be.

4+2=6
4-2=2
4*2=8
4/2=2.00
4//2=2
4%2=0
4**2=16

For each part, your output must look exactly as displayed in the sample output, otherwise, you
will lose 5 points for that part.

Finished: Don’t forget to save your file and turn it in via Canvas as an attachment to Lab2.
Lab2yourlastname.py

You might also like