Skip to content

Commit b0ef0a0

Browse files
Add number base converter in python
1 parent 46f18ec commit b0ef0a0

File tree

1 file changed

+36
-0
lines changed

1 file changed

+36
-0
lines changed

number_base_converter.py

+36
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
####################################################################################################
2+
3+
# Question
4+
# Write a Python script that reads any integer and asks the user to choose which option to convert:
5+
# 1 for Binary
6+
# 2 for Octal
7+
# 3 for Hexadecimal
8+
9+
####################################################################################################
10+
num = 0
11+
num = int(input("\nDigit an integer positive or -1 to finish: "))
12+
13+
while num != -1:
14+
print("""
15+
Choose one of the bases to convert:
16+
17+
[ 1 ] convert to BINARY
18+
[ 2 ] convert to OCTAL
19+
[ 3 ] convert to HEXADECIMAL
20+
""")
21+
22+
option = int(input("Your choose: "))
23+
24+
if option == 1:
25+
print(f"\n{num} converted to BINARY is {bin(num)[2:]}")
26+
elif option == 2:
27+
print(f"\n{num} converted to OCTAL is {oct(num)[2:]}")
28+
elif option == 3:
29+
print(f"\n{num} converted to HEXADECIMAL is {hex(num)[2:].upper()}")
30+
else:
31+
print("\n #### Invalid option. Try again. ####")
32+
33+
num = int(input("\nDigit an integer positive or -1 to finish: "))
34+
35+
print("Bye!!!")
36+
####################################################################################################

0 commit comments

Comments
 (0)