Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

Python 3, include docstrings. Idea is to create a calculator Devclop the following Python program: a tool to convert binary numbers to decimal, and decimal

image text in transcribed

Python 3, include docstrings. Idea is to create a calculator

Devclop the following Python program: a tool to convert binary numbers to decimal, and decimal numbers to binary, and a simple text-only menu to add or subtract mbers. In this program binary uwill be respresented as text strings of only 1s or 0s, and should always appear in strings that are multiples of cight characters long (length of 8, or 16, or 21, ....In case the number to be repre deruire the full amount of the length, the leading pusitio e lled with s. Decimal and binary mbers will all be integers. prins oul Ouce both onversion rouie are ready, a simple menu prograshoud be wrillen. I to the screen a menu that can be similar to the following: What do yon want to do? The algorithm to convert binary mbers to docimal is to start at the high cnd, scanning through lhe strin?from left IAD right. Each line il l diaracter is encountered,?le value of 2 raised lhe appropriate power is addedRecall that the last position is 2, or the value of 1. (In an ciglht-bit binary string, the first position is 27, or 128.) l. Enter the first number 2. Enter the second number 3. Add the two numbers togcther 4. Subtract the second number from the tirst 5. Exit thc program The algorithm to convert decimal numbers into hinary is as follows. The user maks a choice by entrin through 4 as desir I e first uber is enteris shown back to the user with appropriate text to explain i I the sccond nmber is entered, both numbers are shown back to the uscr, with text cxplanations. In cither case, the me is displayed again afterwards. When option 3 is scloctd, the two mbers are added together ad the sun displayed. All of thee becrs are to be entered by the uscr as binary text strings, converted to dec'tnal, and addod, ind ? verted back for display on the screen. You may choose to display both docimal and binary on the scree, or only the binary.) when option 1 is sclected, subtraction is pertormed rather than addion. In this situation, care should bc taken to avoid negative numbers. Your program will have to ilude som mcchanism to detoet the possibility of a negative number and responding gracefully should this happen 1. First, divide the number by 2 using integer divison, retaining both the quoticnt and the 2. The remainder bocomcs the last character of the binary string, whcthcr 1 or 0. 3. The otient is then divided by 2 again, using integer division and retaining both the quotient and the remainder 4. This new remainder is pre-lixed to the front ed of the binary string 5. Steps3 and 4 are repeated as often as necessary, until hoth the quotient and remainder ome out as ero, 6. Once the hinary nmher string is completed, snfticient zeros are pre-fixed to the font end to round out a multiple of eight bits n length Kach of the conversion routines should he placed nside of functions to be casily called from the main prog As desired, teyc be spread over several functions, ede ach of them ought to have one main function that is to be called to start the conversion.)

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image_2

Step: 3

blur-text-image_3

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

More Books

Students also viewed these Databases questions

Question

Organize and support your main points

Answered: 1 week ago

Question

Move smoothly from point to point

Answered: 1 week ago

Question

Outlining Your Speech?

Answered: 1 week ago