Skip to content

Course home

Sign up

Develop code

EasyMediumHard
123456789101112131415
Question 4

A program converts a binary pattern to a positive denary integer using a left-to-right doubling algorithm. The user enters the binary pattern as a string. The program loops continually until the user inputs an empty pattern to stop the program.

For each binary digit from left to right, the program doubles the current denary total, and then adds 1 if the binary digit is a '1'. If the digit is '0', it adds 0.

The table shows accurate test results for three inputs.

InputOutput
110113
10010137
<empty>Exits program

The lines of code in the Python program below are mixed up. Note that the correct relative indentation of each line has been preserved.

        if bit == "1":
    print("Denary:", denary)
user_input = input("Enter binary: ")
    for bit in user_input:
while user_input != "":
            denary = denary + 1
    denary = 0
    user_input = input("Enter binary: ")
        denary = denary * 2

Rearrange the lines of code to make the program work and produce the correct output.

Do not change the capitalization or functionality of the given lines of code. Do not add any additional functionality.

[15]
Markscheme

Develop code Questions

  1. GCSE
  2. /Computer Science
  3. /Develop code

51 exam-style questions on Edexcel GCSE Computer Science Develop code, covering Decomposition and abstraction to solve problems, Read, write, analyse and refine programs, Converting algorithms into programs, Techniques for readable, maintainable code, Identifying and correcting program errors, and Evaluating program fitness and efficiency. Each one has a worked solution and a mark scheme showing where the marks go.

Question bank