Question

In: Computer Science

Using Python, create a program that will act as a number convertor. This program will convert...

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.

Solutions

Expert Solution

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:


Related Solutions

USING PYTHON Write a program to create a number list. It will call a function to...
USING PYTHON Write a program to create a number list. It will call a function to calculate the average values in the list. Define main ():                        Declare variables and initialize them                        Create a list containing numbers (int/float)                        Call get_avg function that will return the calculated average values in the list.                                       Use a for loop to loop through the values in the list and calculate avg                        End main()
Python program. Write a python program that can convert any radix-d (arbitrary base) number to the...
Python program. Write a python program that can convert any radix-d (arbitrary base) number to the equivalent radix-e (another arbitrary base) number. Where e and d are members in [2, 16]. Remember, base 16 needs to be calculated as hexadecimal. So, if radix-d is input as a hexadecimal number, it needs to convert and output to desired base. Conversely, if base 16 is the desired output, then the output needs to show a hexadecimal number. Hints: The easiest approach is...
Create design document for a program that will allow the user: Convert a number to binary....
Create design document for a program that will allow the user: Convert a number to binary. Convert from binary to number. Display the hexadecimal representation of a binary number. Given an hexadecimal display its binary representation. Convert a number to IEEE single precision and vice versa. More to come. PLEASE ADD PSEUDOCODE AND USE C PROGRAMMING USE FUNCTIONS IF POSSIBLE
Posting together because they are related. Python 1. Create a program to count the number of...
Posting together because they are related. Python 1. Create a program to count the number of occurrences of each letter of the alphabet in a text file, and print out the total for each letter (if greater than 0). Ignore numbers, punctuation, and special characters. Make sure to treat capital and lowercase letters as occurrences of the same letter. The text file (sample_text.txt) is attached to the assignment. Example: For the text "My dog's name is Winston." The results would...
In python using tkinter, I want to create a program. The program can have various classes...
In python using tkinter, I want to create a program. The program can have various classes and methods but I want it to have circles, triangles, and squares. Each shape movies in a certain way, like circles can move linearly, triangles can be affected by gravity, and squares can only go up and down. If the shapes get too close to one another then they disappear and a new shape appears somewhere else. I want this program to run continuously.
Create a PYTHON program that lets a user guess a number from 1 to 1,000: -i...
Create a PYTHON program that lets a user guess a number from 1 to 1,000: -i used random.randint(1,1000) -must give hints like (pick a lower/higher number) which i already did -tell the user when he has repeated a number (help) -the game only ends when you guess the number (help) -you loose $50 every failed attempt (done) ****I did it as a while loop -
Create a Python script in IDLE or Kali Python3 CLI to create the following Python Program:...
Create a Python script in IDLE or Kali Python3 CLI to create the following Python Program: Your program will create a username of your choice using a Kali Linux command "useradd -m mark", then set the password for that user using the Kali Linux Command "echo "mark:10101111" | chpasswd". Then you create a dictionary file using the Kali Linux command "crunch 8 8 01 > mylist.txt" Your python script should crack the password for that user and display on the...
java program Create a program that creates and prints a random phone number using the following...
java program Create a program that creates and prints a random phone number using the following format: XXX-XXX-XXXX. Make sure your output include the dashes.  Do not let the first three digits contain an 8 or 9 (HINT: do not be more restrictive than that) and make sure that the second set of three digits is not greater than 773. Helpful Hint:   Think though the easiest way to construct the phone number. Each digit does do not have to be determined...
Create a c program that takes 1 parameter, a number using that number, dynamically allocate a...
Create a c program that takes 1 parameter, a number using that number, dynamically allocate a memory so you store that number of integers write integers in order starting from 1 until you fill all that memory print the address and values of the first and the last integer stored in the memory
Create a Linear Program solver using Matlab or Python. Prompt variable and constraint entry.
Create a Linear Program solver using Matlab or Python. Prompt variable and constraint entry.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT