Question

In: Computer Science

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

PYTHON

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

20 pts.

The Menu() function achieves the goals given above.

20 pts.

Base2() takes in an int from the user and accurately converts it to binary

20 pts.

Base10() takes in and accurately converts a binary number to a decimal. The binary number is entered by the user.

20 pts.

Reverse() is implemented and called correctly.

20 pts.

Comments-There should be a comment block at the top that contains:

  • Your name
  • Program name
  • Date
  • Program Description

There should also be a comment before each function explaining

  • Function name
  • Input(parameters)
  • Output(return statements)
  • Description

See reverse above.

Solutions

Expert Solution

Program

Output:

*** Welcome to the conversion program ***
*** Menu ***
1. Convert Binary to Decimal
2. Convert Decimal to Binary
3. Quit

Enter an option (1-3): 1
Enter a Binary number: 1010
Equivalent Decimal number is 10
*** Menu ***
1. Convert Binary to Decimal
2. Convert Decimal to Binary
3. Quit

Enter an option (1-3): 2
Enter a Decimal number: 14
Equivalent Binary number is 1110
*** Menu ***
1. Convert Binary to Decimal
2. Convert Decimal to Binary
3. Quit

Enter an option (1-3): 3

Process finished with exit code 0


Related Solutions

Create a menu-based program that will allow the user to calculate the area for a few...
Create a menu-based program that will allow the user to calculate the area for a few different shapes: square, rectangle, parallelogram, and circle. Refer to the Sample Output in this document to see what menu options you should use. Create PI as a global constant variable. Main Function use do-while to repeat program until user chooses option 5 call the displayMenu function get user choice & validate with while loop depending on user’s choice, get the data required to calculate...
Create a menu-based program that will allow the user to calculate the area for a few...
Create a menu-based program that will allow the user to calculate the area for a few different shapes: square, rectangle, parallelogram, and circle. Refer to the Sample Output in this document to see what menu options you should use. Create PI as a global constant variable. Main Function use do-while to repeat program until user chooses option 5 call the displayMenu function get user choice & validate with while loop depending on user’s choice, get the data required to calculate...
We need to create basic program that will simply display a menu and allow the user...
We need to create basic program that will simply display a menu and allow the user to select one of the choices. The menu should be displayed such that each option will be numbered, as well as have one capital letter so as to indicate that either the number or the designated letter can be entered to make their choice. Once the choice is made, the sub-menu for that selection should be displayed. Colors with an odd number of letters...
Design and implement a Python program which will allow two players to play the game of...
Design and implement a Python program which will allow two players to play the game of Tic-Tac-Toe in a 4x4 grid! X | O | X | O -------------- O | O | X | O -------------- X | X | O | X -------------- X | X | O | X The rules for this game is the same as the classic, 3x3, game – Each cell can hold one of the following three strings: "X", "O", or "...
Write a program names EncryptDecrypt.java that has the following menu choices: Print menu and allow user...
Write a program names EncryptDecrypt.java that has the following menu choices: Print menu and allow user to choose options. The program must have a file dialogue box for text file. Output should be based on user choices. Read in a file Print the file to the console Encrypt the file and write it to the console Write out the encrypted file to a text file Clear the data in memory Read in an encrypted file Decrypt the file Write out...
Write a GUI based Python program that will allow the user to perform (i) generate RSA...
Write a GUI based Python program that will allow the user to perform (i) generate RSA keys, (ii) encrypt a given message and (iii) decrypt the message without using any cryptographic library. Your program will generate the values of p unless the user provides them manually. Then the program will generate the keys (private and public). Then the program will allow the user to enter his/her message to be encrypted. Finally, the program will perform the decryption operation as well....
I'm making a python program that checks the users input. If the users input does not...
I'm making a python program that checks the users input. If the users input does not match the rules then the program will output "No". If the users input does match the rules it will output "Yes". The rules are : at least 5 uppercase letters at least 5 lowercase letters at least 5 numbers No more than 20 characters in total I have managed to meet these conditions in individual python files but not in one. Ideally without importing...
I'm making a python program that checks the users input. If the users input does not...
I'm making a python program that checks the users input. If the users input does not match the rules then the program will output "No". If the users input does match the rules it will output "Yes". The rules are : 5 uppercase letters 5 lowercase letters 5 numbers First letter must be capitilized The total characters must be 15 I have managed to meet these conditions in individual python files but not in one. Ideally without importing anything, still...
Write a python program that will allow a user to draw by inputting commands. The program...
Write a python program that will allow a user to draw by inputting commands. The program will load all of the commands first (until it reaches command "exit" or "done"), and then create the drawing. Must include the following: change attributes: color [red | green | blue] width [value] heading [value] position [xval] [yval] drawing: draw_axes draw_tri [x1] [y1] [x2] [y2] [x3] [y3 draw_rect [x] [y] [b] [h] draw_poly [x] [y] [n] [s] draw_path [path] random random [color | width...
C++ Write a menu based program for the pet rescue. There should be 2 menu options...
C++ Write a menu based program for the pet rescue. There should be 2 menu options -Add a pet -View pets -If the user chooses option 1, you will open a data file for writing without erasing the current contents, and write a new pet record to it. The file can be formatted any way that you choose but should include the pet's name, species, breed, and color. You my also include any additional information you think is appropriate. -If...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT