Denary to Binary Conversion Algorithm

In this challenge, we are going to write al algorithm to convert a decimal (aka Denary) number between 0 and 255 into binary using 1 Byte (=8 bits).

Before attempting this challenge you may practise your denary to binary conversion:

Python Challenge


We are now going to use some Python code to implement al algorithm to convert a denary number into binary. To do so, you will need to complete the following 4 tasks.

Python Challenge - Task #1Task #2: Input ValidationTask #3: Writing a FunctionTask #4: Binary to Denary

Python Challenge: Task #1

We have started an algorithm which we would like you to complete to perform a full denary to binary conversion.

Your first challenge is to investigate the code below to see what it does and how it works. You will then need to complete this code to make sure that it outputs a fully Byte of data for any given number between 0 and 255.

The following trace table may help you investigate this code step by step:

 Line NumberdenarybinaryOUTPUT
3

Python Challenge: Task #2

Add some code to your code from task 1 to validate the input so that your code only accepts a number between 0 and 255. You can find out more about different validation techniques below and select the validation technique that is most relevant to this task:

Validation Techniques:


Input Validation: Presence Check

name = input("Enter your name:").strip() 

if name=="":
    print("Empty name!")
else:
    print("Thank you!")

Input Validation: Type Check – Integer?

number = input("Type a number:")

if number.isdigit():
    print("This is a number")
else:
    print("This is not a whole number")

Input Validation: Range Check

number = int(input("Type a number between 1 and 5:"))

if number>=1 and number<=5:
    print("Valid number")
else:
    print("Invalid number")

Input Validation: Lookup Check

drive = input("Can you drive?").lower()

if drive in ["yes","no"]:
    print("Valid answer")
else:
    print("Invalid answer")

Input Validation: Character Check

email = input("Type your e-mail address:")

if "@" in email:
    print("Valid e-mail address")
else:
    print("Invalid e-mail address")
postcode = input("Type your postocode:").upper()

if postcode[0] in "ABCDEFGHIJKLMNOPQRSTUVWXYZ" and postcode[1] in "ABCDEFGHIJKLMNOPQRSTUVWXYZ" and postcode[2] in "123456789":
    print("Valid postcode")
else:
    print("Invalid postcode")

Input Validation: Length Check

password = input("Type a password:")

if len(password)>=8:
    print("Valid password")
else:
    print("Invalid password")

Try Again! Using a While Loop:

name = input("Enter your name:")

while name=="":
    print("You must enter your name! Please try again!")
    name = input("Enter your name:")

print("Welcome " +  name)

Task #3: Writing a Function

Turn your code into a function called convertToBinary(). Your function will take a denary number as a parameter and return a binary string. You will then need to add some code to test your function.​

Task #4: Writing a Function

Create a new function called convertToDenary(). This new function will take a binary string as a parameter and return the matching denary value.


unlock-access

Solution...

The solution for this challenge is available to full members!
Find out how to become a member:
➤ Members' Area

Did you like this challenge?

Click on a star to rate it!

Average rating 3.3 / 5. Vote count: 60

No votes so far! Be the first to rate this post.

As you found this challenge interesting...

Follow us on social media!