Question

Hi, I need help writing a code for this. The language is python 3, and we...

Hi, I need help writing a code for this. The language is python 3, and we cannot use things like break, continue, exit(), lambda, map, filter, raise, try, except, and assert in our code. Thank you!

We must write a function called "binary_to_decimal(binary_number)" that takes a string for a binary number and output the decimal integer for that number.

The solution, aka the code, MUST contain a for loop of this form (power is an integer variable you define earlier):

which is "for i in range(power, -1, -1):"

We CANNOT use any other loops in our function.

Now the following main block:

if __name__ == '__main__':

   print(binary_to_decimal('11001111'))

   print(binary_to_decimal('00000011'))

   print(binary_to_decimal('100000000011'))

   print(binary_to_decimal('1'))

Will print:

207

3

2051

1

0 0
Add a comment Improve this question Transcribed image text
Answer #1

Program :

import math
def binary_to_decimal(binary):
decimal = 0;
power = len(binary)-1
binary = binary[::-1]
for i in range(power, -1, -1):
rem = (int(binary)%10)
decimal = decimal+rem*math.pow(2,power)
power = power-1
binary = int(binary)/10
return int(decimal)

if __name__ == '__main__':
print(binary_to_decimal('11001111'))
print(binary_to_decimal('00000011'))
print(binary_to_decimal('100000000011'))
print(binary_to_decimal('1'))
  

Screenshot of the program :

1 import math 2- def binary_to_decimal(binary): 3 decimal = 0; 4. power = len(binary)-1 5 binary = binary[::-1] 6 for i in ra

NOTE: If you want to change something , please let me know through comments; I will surely revert back to you.

Please give a up vote .....

Add a comment
Know the answer?
Add Answer to:
Hi, I need help writing a code for this. The language is python 3, and we...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • Hi, I need help writing a code for this. The language is python 3, and we...

    Hi, I need help writing a code for this. The language is python 3, and we cannot use things like break, continue, exit(), lambda, map, filter, raise, try, except, and assert in our code. Thank you! Implement one of the sorting algorithms from class, either bubble, selection or quick sort with the following modification. Call the function "color_sort(the_list)" Each element is now a list (or tuple) with two elements. The first element is the number value that you need to...

  • Need help with Python (BinarySearch), code will be below after my statements. Thank you. Have to...

    Need help with Python (BinarySearch), code will be below after my statements. Thank you. Have to "Add a counter to report how many searches have been done for each item searched for." Have to follow this: 1) you'll create a counter variable within the function definition, say after "the top = len(myList)-1" line and initialize it to zero. 2) Then within the while loop, say after the "middle = (bottom+top)//2" line, you'll start counting with "counter += 1" and 3)...

  • Please answer this question using python programming only and provide a screen short for this code....

    Please answer this question using python programming only and provide a screen short for this code. Homework 3 - Multiples not less than a number Question 1 (out of 3) This homework is a SOLO assignment. While generally I encourage you to help one another to learn the material, you may only submit code that you have composed and that you understand. Use this template to write a Python 3 program that calculates the nearest multiple of a number that...

  • Hi, I need some help finishing the last part of this Python 1 code. The last...

    Hi, I need some help finishing the last part of this Python 1 code. The last few functions are incomplete. Thank you. The instructions were: The program has three functions in it. I’ve written all of break_into_list_of_words()--DO NOT CHANGE THIS ONE. All it does is break the very long poem into a list of individual words. Some of what it's doing will not make much sense to you until we get to the strings chapter, and that's fine--that's part of...

  • Python help! Any help is appreciated, thank you! Fill in the missing function, monthString(), in the...

    Python help! Any help is appreciated, thank you! Fill in the missing function, monthString(), in the program. The function should take number between 1 and 12 as a parameter and returns the corresponding month as a string. For example, if the parameter is 1, your function should return "January". If the parameter is 2, your function should return out "February", etc. def monthString(monthNum): """ Takes as input a number, monthNum, and returns the corresponding month name as a string. Example:...

  • Write a python code that takes in an number 0-9 and prints out the word of...

    Write a python code that takes in an number 0-9 and prints out the word of the number. For example 1 would print out one. Below is the skeleton of the code that needs to be filled in. def num2string(num): """ Takes as input a number, num, and returns the corresponding name as a string. Examples: num2string(0) returns "zero", num2string(1)returns "one" Assumes that input is an integer ranging from 0 to 9 """ numString = "" ################################### ### FILL IN...

  • I need to rewrite this code without using lambda function using python. def three_x_y_at_one(x): result =...

    I need to rewrite this code without using lambda function using python. def three_x_y_at_one(x): result = (3 * x *1) return result three_x_y_at_one(3) # 9 zero_to_four = list(range(0, 5)) def y_values_for_at_one(x_values): return list(map(lambda x : three_x_y_at_one(x), x_values)) -------> this is the function that I need to rewrite without using a lambda function . y_values_for_at_one(zero_to_four) # [0, 3, 6, 9, 12] Thanks

  • Need help writing this code in python. This what I have so far. def display_menu(): print("======================================================================")...

    Need help writing this code in python. This what I have so far. def display_menu(): print("======================================================================") print(" Baseball Team Manager") print("MENU OPTIONS") print("1 - Calculate batting average") print("2 - Exit program") print("=====================================================================")    def convert_bat(): option = int(input("Menu option: ")) while option!=2: if option ==1: print("Calculate batting average...") num_at_bats = int(input("Enter official number of at bats: ")) num_hits = int(input("Enter number of hits: ")) average = num_hits/num_at_bats print("batting average: ", average) elif option !=1 and option !=2: print("Not a valid...

  • I need help writing a very basic code in python. The code should not be very...

    I need help writing a very basic code in python. The code should not be very complicated. The code must have the following things in it: 1. create a function that takes a state and length of characters and makes a license plate and use the function to make a plate based on user input (use the def function and random.choice as well as import random) 2. the license plate should be a mix of random letters and numbers based...

  • Python Language Only!!!! Python Language Only!!!! Python Language Only!!!! Python 3 is used. A. Write a...

    Python Language Only!!!! Python Language Only!!!! Python Language Only!!!! Python 3 is used. A. Write a function named smaller that accepts two arguments: a list, and a number n. Assume that the list contains numbers. The function should display all numbers in the list that are smaller than the number n. Write a code that declares and initializes a list of numbers and a variable number and pass them to the function to test it. Please see the outcome below:...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT