У меня был код, очень похожий на то, что вам нужно.Обратите внимание, что вы не проверили правильность ввода от пользователя.
надеюсь, это поможет
import math
def isfloat(value):
"""
Checks if the given value represent float
:param value:
:return: True if float
"""
try:
float(value)
return True
except:
return False
def chooseOperator(input):
# Returns a method to run
return {
'1': oneAdd,
'add': oneAdd,
'2': twoDif,
'subtract': twoDif,
'3': threeMult,
'multiply': threeMult,
'4': fourDiv,
'divide': fourDiv,
'5': fiveSqrt,
'sqaure root': fiveSqrt,
'6': sixExit,
'exit': sixExit
}[input]
def printMenu():
print('\n\t-- MENU --')
print('1)\t Add')
print('2)\t Subtract')
print('3)\t Multiply')
print('4)\t Divide')
print('5)\t Square Root')
print('6)\t Exit')
def mainLoop():
inputFromUser = True
print('\n\n** Python Calculator Program **')
print('CALCULATOR: [ON] OFF')
while inputFromUser:
# Prints the menu to the console
printMenu()
try:
# operator is a function
operator = chooseOperator((input('Choose an operator: ')).lower())
# inputFromUser is a boolean variable
inputFromUser = operator()
except KeyError:
# Unknown input
print('\n\t Please choose an operator from the menu')
def oneAdd():
# Get input from user
a = input('Enter the first number that you wish to add: ')
b = input('Enter the second number that you wish to add: ')
# Check that the input is valid
if isfloat(a) and isfloat(b):
# Calculate with values
ans = float(a) + float(b)
print('The sum of the numbers are: ', ans)
else:
# Notify the user that the values are not valid
print("\tInvalid values:")
print("\t\tfirst = ", a)
print("\t\tsecond = ", b)
return True
def twoDif():
# Get input from user
a = input('Enter the first number that you wish to subtract: ')
b = input('Enter the second number that you wish to subtract: ')
# Check that the input is valid
if isfloat(a) and isfloat(b):
# Calculate with values
ans = float(a) - float(b)
print('The difference of the numbers are: ', ans)
else:
# Notify the user that the values are not valid
print("\tInvalid values:")
print("\t\tfirst = ", a)
print("\t\tsecond = ", b)
return True
def threeMult():
# Get input from user
a = input('Enter the first number that you wish to multiply: ')
b = input('Enter the second number that you wish to multiply: ')
# Check that the input is valid
if isfloat(a) and isfloat(b):
# Calculate with values
ans = float(a) * float(b)
print('The product of the numbers are: ', ans)
else:
# Notify the user that the values are not valid
print("\tInvalid values:")
print("\t\tfirst = ", a)
print("\t\tsecond = ", b)
return True
def fourDiv():
# Get input from user
a = input('Enter the dividend: ')
b = input('Enter the divisor: ')
# Check that the input is valid
if isfloat(a) and isfloat(b):
# Calculate with values
ans = float(a) / float(b)
print('The quotient of the numbers are: ', ans)
else:
# Notify the user that the values are not valid
print("\tInvalid values:")
print("\t\tfirst = ", a)
print("\t\tsecond = ", b)
return True
def fiveSqrt():
# Get input from user
a = input('Enter the number you wish to find the square root of: ')
# Check that the input is valid
if isfloat(a):
# Calculate with values
ans = math.sqrt(float(a))
print('The square root of the number is: ', ans)
else:
# Notify the user that the values are not valid
print("\tInvalid value:")
print("\t\tfirst = ", a)
return True
def sixExit():
print('\n\nCALCULATOR: ON [OFF]')
return False
if __name__ == '__main__':
mainLoop()