This is a problem set. Some of these are easy, others are far more difficult. The purpose of these problems sets are:
- to build your skill applying computational thinking to a problem
- to assess your knowledge and skills of different programming practices
What is this problem set trying to do
The actual solution to this problem is fairly straight-forward, but imagining how to solve the problem is tricky. As a side note, you must understand how to convert base 10 to base 2.
Ask the user to enter a number in base 10. Then convert the number the user entered into binary. Extra points if you ensure the binary always has 8 digits. You must not use built-in math functions, but rather construct the solution on your own.
- User Input: 1
- Expected output: 1
- User Input: 4
- Expected output: 100
- User Input: 8
- Expected output: 1000
How you will be assessed
Your solution will be graded using the following axis:
- To what extent does your code implement the features required by our specification?
- To what extent is there evidence of effort?
- To what extent did your code meet specifications?
- To what extent did your code meet unit tests?
- To what extent is your code free of bugs?
- To what extent is your code written well (i.e. clearly, efficiently, elegantly, and/or logically)?
- To what extent is your code eliminating repetition?
- To what extent is your code using functions appropriately?
- To what extent is your code readable?
- To what extent is your code commented?
- To what extent are your variables well named?
- To what extent do you adhere to style guide?
A possible solution
Click the expand link to see one possible solution, but NOT before you have tried and failed!
This code was donated to our wiki by an 11th grade student, Jake. I am very grateful to him for the code.
#first off, we need something to convert (duh), so we are asking the user to feed a base 10 number to the program by using the input function number = int(input("What base 10 nr would you like to convert to binary? Please enter it below \n > ")) #setting an empty string. This is where the binary equivalent of the above input will be binary = "" #getting to the mathy part. This system was inspired by the tutorial over from https://www.electronics-tutorials.ws/binary/bin_2.html, where it tells you to just keep diving the number by 2 until you get it down to 0. while number != 0: #checking if the number has a remainder if number%2 == 1: #if the number DOES have a remainder, we need to a 1 in our binary string (cause that's the remainder) binary += "1" # rounding the number down because decimals would mess this up number = int(number/2) # checking if the number does not have a remainder elif number %2 == 0: #if the number DOESN'T have a remainder, we need to a 0 in our binary string (cause there's no remainder) binary += "0" # rounding the number down because decimals would mess this up number = int(number/2) #this part was grabbed over from https://www.educative.io/edpresso/how-do-you-reverse-a-string-in-python. These are the 2nd and 3rd from the last block of code on the page #this is needed because according to the tutorial, the result of the first time we divide the number (either a 0 or 1) is the "Least Significant Bit" and the result of the last time we divied is the "Most Significant Bit" #we gotta swap it because it's backwards right now. It's from LSB to MSB right now, and it needs to be from MSB to LSB reversed =''.join(reversed(binary)) #printing the result print(reversed)