Skip to content

Commit 545063b

Browse files
Pull Request TheAlgorithms#1 for Hacktoberfest
1 parent 71b372f commit 545063b

File tree

1 file changed

+83
-0
lines changed

1 file changed

+83
-0
lines changed

Python Menu.py

+83
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,83 @@
1+
def add():
2+
num1 = float(input("Enter a number: "))
3+
num2 = float(input("Enter a second number: "))
4+
res = num1 + num2
5+
print("The result is:", res)
6+
7+
def subtract():
8+
num1 = float(input("Enter a number: "))
9+
num2 = float(input("Enter a second number: "))
10+
res = num1 - num2
11+
print("The result is:", res)
12+
13+
def multi():
14+
num1 = float(input("Enter a number: "))
15+
num2 = float(input("Enter a second number: "))
16+
res = num1 * num2
17+
print("The result is:", res)
18+
19+
def divide():
20+
num1 = float(input("Enter a number: "))
21+
num2 = float(input("Enter a second number: "))
22+
res = num1 / num2
23+
print("The result is:", res)
24+
25+
def decbin():
26+
decimal = float(input("Enter a number: "))
27+
binario = ""
28+
cociente = int(decimal)
29+
while cociente != 1 :
30+
residuo = cociente % 2
31+
cociente = cociente // 2
32+
binario = binario + str(residuo)
33+
34+
binario = binario + str(residuo)
35+
res = binario [::-1]
36+
37+
print(res)
38+
39+
def bindec():
40+
binario = input("Enter a binary number: ")
41+
decimal = 0
42+
longitud = len(binario)
43+
contador = 0
44+
binario_rev = binario[::-1]
45+
while contador < longitud:
46+
if binario_rev[contador] == "1":
47+
decimal += 2**contador
48+
contador +=1
49+
50+
print( decimal)
51+
52+
def exit():
53+
print("See you!")
54+
sys.exit()
55+
56+
while True:
57+
print("Option Menu")
58+
print("1. Convert from binary to decimal")
59+
print("2. Convert from decimal to binary")
60+
print("3. Multiply two numbers")
61+
print("4. Divide two numbers")
62+
print("5. Add two numbers")
63+
print("6. Subtract two numbers")
64+
print("7. Exit")
65+
66+
opcion = input("Enter your option: ")
67+
68+
if opcion == "1":
69+
bindec()
70+
elif opcion == "2":
71+
decbin()
72+
elif opcion == "3":
73+
multi()
74+
elif opcion == "4":
75+
divide()
76+
elif opcion == "5":
77+
add()
78+
elif opcion == "6":
79+
subtract()
80+
elif opcion == "7":
81+
exit()
82+
else:
83+
print("Invalid Option")

0 commit comments

Comments
 (0)