Question

In this lab we will design a menu-based program. The program will allow users to decide...

In this lab we will design a menu-based program. The program will allow users to decide if they want to convert a binary number to base 10 (decimal) or convert a decimal number to base 2 (binary). It should have four functions menu(), reverse(), base2(), and base10(). Each will be outlined in detail below. A rubric will be included at the bottom of this document.

Menu()

The goal of menu() is to be the function that orchestrates the flow of the program. menu() should be the first thing called and should do the following:

  • Print a welcome message
  • Print the following options:
    • 1. Convert Binary to Decimal
    • 2. Convert Decimal to Binary
    • 3. Quit
  • Prompt the user for input
  • Evaluate the input and execute the appropriate function call, break, or print “Invalid input” based as on the user input.
  • The menu should continue looping until the user quits

Base2()

This function should take a number from the user and convert it into its binary representation then print it to the user. You can use your code from Lab1.

Base10()

This function should prompt the user for a binary number represented by a string and calculate the decimal number. Base10() should do the following:

  • Prompt user for binary number
  • Declare a sum variable and set it to zero
  • Store the return value of a function call to reverse() (included below) into a variable (for the sake of example let’s call it st0.
  • Use:

for i in range(0,len(st)):

            sum+=(int(st[i])*(2**i))

print(sum)

Remember that len returns the number of characters in a string and we can access a character by using the string name and an index number. st[0] would return the first character of the string in variable st. If we think about the conversion, we take the number and multiple by 2 raised to the power of the position number. If we reverse the string, we can use the index number as this position number because the string is indexed left-to-right. Which is the opposite of number positions are right-to-left. Thus, I can be used as our exponent.

Reverse()

#Name: Reverse

#Input: String s

#Output: String r

#Description: This function takes a string as input and reverses that string

#returning it as output.

def reverse(s):

r=""

for i in s:

    r=i+r

return r

Homework Answers

Answer #1

If you have any doubts, please give me comment...

def menu():

    print("Convert a binary number to base 10 (decimal) or convert a decimal number to base 2 (binary)")

    print("1. Convert Binary to Decimal")

    print("2. Convert Decimal to Binary")

    print("3. Quit")

def base2():

    num = int(input("Enter decimal number: "))

    st = ""

    while num>0:

        st = str(num%2)+st

        num = num//2

    print("In Binary",st)

def base10():

    st = input("Enter binary number: ")

    st = reverse(st)

    sum = 0

    for i in range(0,len(st)):

        sum+=(int(st[i])*(2**i))

    print("In Decimal:",sum)

def reverse(s):

    r=""

    for i in s:

        r=i+r

    return r

def main():

    while True:

        menu()

        choice = int(input("Enter choice:"))

        if choice==1:

            base10()

        elif choice==2:

            base2()

        elif choice==3:

            break

        else:

            print("Invalid input")

        print()

if __name__ == "__main__":

    main()

Know the answer?
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
Similar Questions
17.19 Lab 8B: printing a binary number This lab asks you to use string formatting to...
17.19 Lab 8B: printing a binary number This lab asks you to use string formatting to print a binary number. First you will prompt the user for an integer, using exactly the prompt "Enter a decimal integer: " including the colon character and a space following it. Then print out the decimal number and it's binary equivalent. You do not need to do any mathematical conversion of the integer to binary, you can use the format method. For example, if...
Write a program that reads a binary string (string of 0’s and 1’s) converting the binary...
Write a program that reads a binary string (string of 0’s and 1’s) converting the binary value into decimal. Allow the user to type in as many numbers as they want (one at a time) and end the program when they type in “0”. Use Horner’s method (given in step 3, below) to convert from binary to decimal. Sample Run Binary to Decimal Conversion Enter a binary string: 1101 1101 = 13 decimal Enter a binary string: 10011001 10011001 =...
design a program that prompts the user to enter a positive integer number and reads the...
design a program that prompts the user to enter a positive integer number and reads the user’s input. If the user input is not a positive number, the program should prompt them repeatedly until they enter a valid input. Once a valid input is received ,the program uses a loop to calculate the sum of the digits of the user’s input and displays the sum. For example, if the user enters the number 94311, the program should print the sum...
3. Create a program which allows the user to swap individual words in an input string....
3. Create a program which allows the user to swap individual words in an input string. Given a string of input, your program should count and store the number of words in a 2D array. The user can then select the index of the words they want to swap. Your program should swap those two words and then print the entire string to ouput. • Use string prompt "> ". • Use indices prompt "Enter two indices: ". • Remove...
Write a Java program that prompts the user to input a word (String). The program must...
Write a Java program that prompts the user to input a word (String). The program must print the reversed word with all consecutive duplicate characters removed. The program must contain the following classes: - The StackX class (you can use the Java Stack class). - The Reverse class which must contain a private data field called “word” of type string, a constructor, and a void method called revNoDup(). The revNoDup() method must reverse the word and remove the consecutive duplicate...
Description: The purpose of this assignment is to practice writing code that calls functions, and contains...
Description: The purpose of this assignment is to practice writing code that calls functions, and contains loops and branches. You will create a C program that prints a menu and takes user choices as input. The user will make choices regarding different "geometric shapes" that will be printed to the screen. General Comments: Your code must contain at least one of all of the following control types: nested for() loops a while() or a do-while() loop a switch() statement an...
You will write a program that loops until the user selects 0 to exit. In the...
You will write a program that loops until the user selects 0 to exit. In the loop the user interactively selects a menu choice to compress or decompress a file. There are three menu options: Option 0: allows the user to exit the program. Option 1: allows the user to compress the specified input file and store the result in an output file. Option 2: allows the user to decompress the specified input file and store the result in an...
I am to create three different versions of the following C program code below that implements...
I am to create three different versions of the following C program code below that implements the conversion of unsigned binary numbers into decimal (Base 2 to Base 10 conversion). Version 1: Complete the C program ”bin2dec ver1.c” that implements binary to decimal conversion. The maximum number of binary bits is 32. The program is made of the functions ”unsigned binary to decimal(const char *str)”and ”main”. The parameter ”str” passed to this function points to a C string comprising only...
Write a program display the following menu: Ohms Law Calculator 1. Calculate Resistance in Ohms 2....
Write a program display the following menu: Ohms Law Calculator 1. Calculate Resistance in Ohms 2. Calculate Current in Amps 3. Calculate Voltage in volts 4. Quit Enter your choice (1-4) If the user enters 1, the program should ask for voltage in Volts and the current in Amps. Use the following formula: R= E/i Where: E= voltage in volts I= current in amps R= resistance in ohms If the user enters 2 the program should ask for the voltage...
This program will output a right triangle based on user specified height triangle_height and symbol triangle_char....
This program will output a right triangle based on user specified height triangle_height and symbol triangle_char. (1) The given program outputs a fixed-height triangle using a * character. Modify the given program to output a right triangle that instead uses the user-specified triangle_char character. (1 pt) (2) Modify the program to use a loop to output a right triangle of height triangle_height. The first line will have one user-specified character, such as % or *. Each subsequent line will have...