A run of the program should prompt the user to enter a number, the base of the number, and the base to convert to the number to, and then print the results of converting the number to decimal, and then the decimal to the output base. Here’s an example of output if the number to be converted is Hex 23B4 to Octal:
So a general process flow is:
⦁ Ask for number to convert
⦁ Ask for base of number
⦁ Ask for base to convert to
⦁ Set decimal to return of “toDecimal” passing the number-in and base-in
⦁ Print message number-in base base-in is decimal decimal-value
⦁ Set number-out to return of “fromDecimal” passing decimal and base-out
⦁ Print message decimal is number-out in base base-out
Outline of the fromDecimal function:
⦁ Define a string number-out and initialize it to an empty string, i.e. ‘’ or “” (this will allow us to concatenate digits to it in a loop)
⦁ Set a quotient variable to the decimal passed in
⦁ Create while loop that checks for quotient great than zero
⦁ Set a remainder variable to quotient mod base-out
⦁ Set next-digit to return of valueToDigit passing in the remainder
⦁ Set a new-quotient variable to quotient integer-division base-out
⦁ Print this loop as: quotient / base-out = new-quotient , next-digit
⦁ Append next digit to front of number-out (e.g. sNumberOut = nextDigit + sNumberOut)
⦁ Set quotient to new-quotient
⦁ (Loop will continue till quotient is zero)
⦁ Return number-out
What I have tried:
# Define Decimal, and Part Two.
def toDecimal(sNumber, nBase):
decimal = 0
# Collect Data for the Conversion.
for i in range(len(sNumber)):
digitPos = len(sNumber) - 1 - i
digit = sNumber[digitPos]
digitVal = digitToValue(digit, nBase)
if (digitVal == -1):
return -1
posVal = nBase ** i
product = digitVal * posVal
print(digit, "x", nBase, "^", i, "=", digitVal, "x", posVal, "=", product)
decimal += product
return decimal
numOut = ""
quotient = decimal
while quotient > 0
quotient = quotient%2
# Print Result
decimalOut = toDecimal(numIn, baseIn)
print(decimalOut)
print(numIn,"base",baseIn,"is decimal",decimalOut)
numberOut = fromDecimal(decimal,baseOut)
print("decimal",decimal,"is",numOut,"in base",baseOut)