Binary to Decimal Updated

In my other blog post ‘Binary to Decimal’, I realized what if I had put in a different number than 1 and 0. I tried putting a 2 and the code still worked. So I will be sharing an updated version, so that you can only put in either 1 or 0. Else there will be an error. # make a new function def bintodeci(num): reverse = len(num) -1 exp = 0 total = 0 for index in range(reverse, -1, -1): # if each number is either 0 or 1 else it'll print out an error. if num[index] in '01': total += int(num[index]) * (2**exp) exp += 1 else: print("Error: Invalid binary number") return None # return the value return total # In case I want to import it if __name__ == '__main__': print(f'The variable __name__ is {__name__}') answer = bintodeci("111") print(answer)

January 28, 2025 · 1 min · SourFox

Binary to Decimal

I’m sharing a program that converts binary to decimal in python. # make a new function def bintodeci(num): reverse = len(num) -1 exp = 0 total = 0 # range(start, stop, step) for digit in range(reverse, -1, -1): total += int(num[digit]) * (2**exp) exp += 1 # return the value return total answer = bintodeci("11") print(answer) sample output: 10 bai.

January 23, 2025 · 1 min · SourFox

Assignment 5: Elements and Indices

Today I had to do another assignment which is to create a python program which prints the elements of a list followed by their corresponding indices. Each element and its index must be on the same line seperated by a space. And if the list is empty, print “Empty List”. I’m going to make a list called num and put 1 to 4 inside. num = [1, 2, 3, 4] Then I’ll use the if statement to do: ...

November 11, 2023 · 2 min · SourFox

Assignment 4: Checking if a list is empty

I was given an assignment for the fourth time. My goal was to write a python program that checks if a list is empty or not. If the list is empty, then it’ll print empty, if not, it’ll print not empty. I’m going to start with an empty list. emp = [] So that I can start the next step. I’m going to use if statement len() else print() When using if, there must be a condition met. My condition is: ...

October 29, 2023 · 1 min · SourFox

Assignment 1: Using Length and String

Today, I was given an assignment. My assignment wanted me to write a Python program that pints the length of a string. This is what I had to print out: The length of "" is 0. The length of "H" is 1. The length of "Hello" is 5. The length of "Amazing" is 7. To do this, I have a plan in my mind which is: Step 1. Find out what I’m going to use. ...

October 15, 2023 · 3 min · Soure Fox