Question 1

The lesson asked you to create a new PythonAnywhere file with the following code:

decimalNum = int(input("Please enter a decimal number: "))
binaryList = []
while decimalNum > 0:
decimalNum = decimalNum / 2
remainder = decimalNum % 2
binaryList.append(remainder)
print(binaryList)

What is the result of running this code and entering 24 as your number?

(1 point)
Responses

[0, 0, 0, 1, 1]
[0, 0, 0, 1, 1]

A huge list of decimal values.
A huge list of decimal values.

0011 0000
0011 0000

24
24
Question 2
The lesson then has you change the code to look like this.

decimalNum = int(input("Please enter a decimal number: "))
binaryList = []
while decimalNum > 0:
remainder = decimalNum % 2
decimalNum = decimalNum // 2
binaryList.append(remainder)
print(binaryList)

What is printed to the console now when running the code and entering 24?

(1 point)
Responses

24
24

[0, 0, 0, 1, 1]
[0, 0, 0, 1, 1]

0001 1000
0001 1000

A huge list of decimal values.
A huge list of decimal values.
Question 3
Using the lesson and the code from the last two questions, what is the binary byte that represents 24?(1 point)
Responses

0010 0010
0010 0010

0001 1000
0001 1000

1100 0000
1100 0000

0000 0011
0000 0011

1 answer

Let's go through each of the questions and determine the answers.

Question 1

The code given is:

decimalNum = int(input("Please enter a decimal number: "))  # Input 24
binaryList = []
while decimalNum > 0:
    decimalNum = decimalNum / 2  # This should be integer division
    remainder = decimalNum % 2
    binaryList.append(remainder)
print(binaryList)
  1. When you enter 24, the first issue with the code is the use of / which performs float division instead of integer division. This means that decimalNum will eventually become a float, and using % on it might not yield the correct remainders as you expect.
  2. The first iteration would be:
    • decimalNum = 24 / 2 = 12.0 and 12.0 % 2 = 0.0 thus appending 0.0.
    • The next iterations would continue dividing by 2 and appending float values until it eventually reaches a value less than or equal to 0.

Thus, if you were to enter 24, the output would be a list of float values that would not correctly represent 24 in binary.

So, the correct printout based on the given options is:

  • [0, 0, 0, 1, 1] (though it should be noted that this output is incorrect as per the intention of the problem due to the float division).

Question 2

The modified code is as follows:

decimalNum = int(input("Please enter a decimal number: "))  # Input 24
binaryList = []
while decimalNum > 0:
    remainder = decimalNum % 2  # Correctly gets the remainder
    decimalNum = decimalNum // 2  # Uses integer division
    binaryList.append(remainder)
print(binaryList)
  1. Now, when you run this code with 24:
    • You get the remainders in reverse order:
      • 24 % 2 = 0 (list is [0])
      • 12 % 2 = 0 (list is [0, 0])
      • 6 % 2 = 0 (list is [0, 0, 0])
      • 3 % 2 = 1 (list is [0, 0, 0, 1])
      • 1 % 2 = 1 (list is [0, 0, 0, 1, 1])
      • The loop stops when decimalNum reaches 0.

Thus, the list [0, 0, 0, 1, 1] is printed.

So, the correct answer is:

  • [0, 0, 0, 1, 1]

Question 3

Now to convert decimal 24 to binary, based on the previous output and the correct understanding, the binary representation of 24 is 0001 1000.

Thus, the answer is:

  • 0001 1000

To summarize:

  1. [0, 0, 0, 1, 1] (for question 1's incorrect code)
  2. [0, 0, 0, 1, 1] (for question 2's corrected code)
  3. 0001 1000 (for the binary representation of 24)