Write a function that inputs a positive decimal value and a base between 2 and 1
ID: 3620215 • Letter: W
Question
Write a function that inputs a positive decimal value and a base between 2 and 16 (inclusive) from the user, and prints the decimal number expressed in that base. I can't use the built in functions that handle bases. Also, i have to incorporate a while loop.Hints: - There are multiple ways to handle the "letter digits" for bases from 11 to 16, but one simple and efficient approach is to index the string "0123456789ABCDEF" in a clever way, such that the numbers 1010 to 1510 are converted to their base-16 letter equivalents.
- You will also need to use a while loop in some way. - If you want to use "equals" in your condition for a while loop, type == (that's two equals signs). If you want to use "not equal", use != (exclamation equals-sign).
After it is executed, it needs to look like this:
>>> from10ToN()
Input the decimal value to convert: 490
Enter the target base, between 2 and 16: 16
1EA
Can someone show me what to do? Write a function that inputs a positive decimal value and a base between 2 and 16 (inclusive) from the user, and prints the decimal number expressed in that base. I can't use the built in functions that handle bases. Also, i have to incorporate a while loop.
Hints: - There are multiple ways to handle the "letter digits" for bases from 11 to 16, but one simple and efficient approach is to index the string "0123456789ABCDEF" in a clever way, such that the numbers 1010 to 1510 are converted to their base-16 letter equivalents.
- You will also need to use a while loop in some way. - If you want to use "equals" in your condition for a while loop, type == (that's two equals signs). If you want to use "not equal", use != (exclamation equals-sign).
After it is executed, it needs to look like this:
>>> from10ToN()
Input the decimal value to convert: 490
Enter the target base, between 2 and 16: 16
1EA
Can someone show me what to do?
Explanation / Answer
bin(x)
Convert an integer number to a binary string. The result is a valid Python expression. If x is not a Python int object, it has to define an __index__() method that returns an integer
hex(x)
Convert an integer number to a hexadecimal string. The result is a valid Python expression. If x is not a Python int object, it has to define an __index__() method that returns an integer
oct(x)
Convert an integer number to an octal string. The result is a valid Python expression. If x is not a Python int object, it has to define an __index__() method that returns an integer.
Related Questions
Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.