In: Computer Science
Using Python, create a program that will act as a number
convertor. This program will convert an input
decimal integer into binary and hexadecimal formats.
a) Define a main function named numberconvertor(). Inside the main
function, write all the
logic of your code. [5% marks]
b) Looping indefinitely (Hint: use infinite while loop), the
program should give the user the
3 options given below and allow the user to select one among them.
[15% marks]
1. converting a decimal number to binary
2. converting a decimal number to hexadecimal
3. exit the program.
c) The program should then ask the user to input a number in the
range from 0 to 1000.
Typecast the input to int value here and save that value in a
variable named decimal. (Hint:
use input() function here) [10% marks]
d) Use range() function to check the number entered by the user as
input. This number should
lie in range from 0 to 1000. If the number entered is in range,
print the statement for
example, “The decimal number 100 is in range”. If the number
entered is not in range, print
“The decimal number entered is outside the given range. Choose your
options again!”. If
either option 1 or option 2 is selected by the user and the number
entered also lies within
the range (0-1000), print the binary or hexadecimal value for the
input number accordingly.
[20% marks]
• Name the file with this formatting:
assignment_num_student_name.py (e.g. assignment5_John_Doe.py)
• Submit your .py file only through Canvas. Do not include any
other file.
• If you are using Jupyter notebook for your code, do not submit
.ipynb file. Go on file→download as→python to
download the .py version of that file.
• If you use Pycharm or Spyder for your code, do not submit the
entire project. Submit only the .py file.
• Comment your name and student number at the top of your file
(e.g. #John Doe # 100666178)
• If you submit a wrong file on Canvas, your assignment will not be
graded. However, if you realize your mistake before
the deadline, you can contact me for resubmission.
• If you do not submit on time (i.e. before 11:59pm Monday), your
assignment will not be graded
• Failure to abide by the submission guidelines will result in mark
deduction (or) no evaluation of the assignment
e) If the user selects option 3 or any other option (excluding 1
and 2), print the statement “End
of program” and allow the user to exit. Use only break to come out
of the infinite while
loop. (If you use sys.exit(), exit() or quit() functions, no marks
will be allotted). [10%
marks]
f) Define two separate recursive functions that convert a decimal
number into a binary and
hexadecimal format as a string (typecast return values to strings).
The functions should be
named as: decimalToBinary(value) and decimalToHex(value). Inside
these functions,
you must write both base and recursive cases. (Keep in mind, if you
write simple
functions, no marks will be allotted. You must define your
functions only recursively)
[30% marks]
g) You must write appropriate function calls for all the functions
in the program including
main function defined in part (a). If you fail to do so, your
grades will be deducted. [10%
marks]
h) You may make other functions wherever applicable, but they will
not be marked.
Python Code:
# Function to print binary number using recursion
def decimalToBinary(value):
if value > 1:
decimalToBinary(value//2) #recursive call
print(value % 2,end = '') #base condition
def decimalToHex(value):
hex = "%X" % value
return fun(hex, 6) #for upto six
def fun(n, length):
if len(n) != length:
return fun('0'+n, length) #recursive call
else:
return n #base condition
def numberconvertor():
while(True):
num=int(input("Enter the number : "))
if num in range(0,1001):
print("\nThe decimal number 100 is in range\n")
else:
print("\nThe decimal number entered is outside the given range. Choose your options again!\n")
print("1. converting a decimal number to binary")
print("2. converting a decimal number to hexadecimal")
print("3. exit the program.")
ch=int(input("\nEnter Choice : "))
if ch==1:
decimalToBinary(num)
print()
elif ch==2:
print(decimalToHex(num))
print()
else:
print("End of program")
break
print()
if __name__ == '__main__':
numberconvertor()
Sample Output: