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.
| Input | Output |
|---|---|
| 1101 | 13 |
| 100101 | 37 |
| <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)\nuser_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.
Practise Edexcel GCSE Computer Science Develop code with exam-style questions for GCSE Computer Science. 57 questions 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, matched to the Edexcel GCSE Computer Science (1CP2) specification and written in Paper 1 and Paper 2 style. Every question includes a full worked solution and mark scheme, so you can see where marks are awarded rather than just whether you got the answer right.