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

Create Restic Backup Program

I’m sharing a wrapper program written in python to backup my files in my device using restic. import subprocess restic_command = ['restic', 'backup'] backup = ["C:\\Users\\veronica\\ibisPaint", "C:\\Users\\veronica\\Pictures", "C:\\Users\\veronica\\Documents"] print("List of directories to be backed up:\n" ) #list the files that will be backed up. #enumerate = allows you to keep track of the number of iterations in a loop. for number, dir in enumerate(backup, start=1): print(f" {number}. {dir}") # Or: print(" " + str(number) + ".", letter ) # \n makes a new line print("\nBackup process started...\n") # combines the lists together. backup_command = restic_command + backup restic = subprocess.run(backup_command, shell=True) The end. -w- ...

December 30, 2024 · 1 min · SourFox

Atm Bill Dispenser

Today, I was given an assignment to write a program to implement an atm dispenser in the following notes: 100, 50, 20, 10, and 5. If the amount is not a multiply of 5, the atm will return a list of 0. In the code are short explanations of why I used it. # asks the customer to enter an amount of number. atm = int(input("Enter an amount in a multiple of 5: ")) # This takes in a amount parameter which is the input from the user and return an array of bills in the following orders: [$100, $50, $20, $10, $5]. def dispenser(amount): # if statement to check if number entered is divisible by 5. If not, return with 0, if yes, continue code. if atm % 5 != 0: return [0, 0, 0, 0, 0] result = [] bill_types = [100, 50, 20, 10, 5] # starts with the starting amount being divided by each number in bill_types. for each_note in bill_types: note_counts = amount // each_note result.append(note_counts) # get the remainder and overwrite the previous. amount = amount % each_note return result # The code below is what will be given back as a sentence to the customer. print(f"You entered ${atm}: {dispenser(atm)}")

August 9, 2024 · 1 min · SourFox

Make a Frequency Dictionary From the Elements Of a List

Halo! Today I have to make a python program that meets these conditions: Write a Python program that creates and print a dictionary that maps each element in a list to its corresponding frequency (how many times it occurs in the list). The test should be case-sensitive. Therefore, “A” should not be considered the same element as “a”. I think for today, I’m just going to make it short. Yea… ...

February 25, 2024 · 1 min · SourFox