python calculator program code example
Example 1: simple python calculator
num1 = input('Enter first number: ')
num2 = input('Enter second number: ')
sum = float(num1) + float(num2)
sum2 = float(num1) - float(num2)
sum3 = float(num1) * float(num2)
sum4 = float(num1) / float(num2)
choice = input('Enter an operator, + = addition, - = subtraction, * = multiplication and / = division: ')
if choice == '+':
print('The sum of {0} and {1} is {2}'.format(num1, num2, sum))
if choice == '-':
print('The sum of {0} and {1} is {2}'.format(num1, num2, sum2))
if choice == '*':
print('The sum of {0} and {1} is {2}'.format(num1, num2, sum3))
if choice == '/':
print('The sum of {0} and {1} is {2}'.format(num1, num2, sum4))
Example 2: calculator python tutorial
while True:
Cal = input("")
print(eval(Cal))
Example 3: python calculator
num_one = int(input("Enter 1st number: "))
op = input("Enter operator: ")
num_two = int(input("Enter 2nd number: "))
if op == "+":
print(num_one + num_two)
elif op == "-":
print(num_one - num_two)
elif op == "*" or op == "x":
print(num_one * num_two)
elif op == "/":
print(num_one / num_two)
Example 4: how to make a calculator using idle
def calculate():
operation = input('''
Please type in the math operation you would like to complete:
+ for addition
- for subtraction
* for multiplication
/ for division
''')
number_1 = int(input('Please enter the first number: '))
number_2 = int(input('Please enter the second number: '))
if operation == '+':
print('{} + {} = '.format(number_1, number_2))
print(number_1 + number_2)
elif operation == '-':
print('{} - {} = '.format(number_1, number_2))
print(number_1 - number_2)
elif operation == '*':
print('{} * {} = '.format(number_1, number_2))
print(number_1 * number_2)
elif operation == '/':
print('{} / {} = '.format(number_1, number_2))
print(number_1 / number_2)
else:
print('You have not typed a valid operator, please run the program again.')
again()
def again():
calc_again = input('''
Do you want to calculate again?
Please type Y for YES or N for NO.
''')
if calc_again.upper() == 'Y':
calculate()
elif calc_again.upper() == 'N':
print('See you later.')
else:
again()
calculate()
Example 5: calculator in python
def mutiply (x):
return 5*x
o = mutiply(10)
print(o)
Example 6: python bmi calculator code
print("Welcome to BMI calculator, by Abthahi Tazrian.")
Offer_Conversions = input("Would you like to convert your imperial units to metric? (yes / no): ")
if Offer_Conversions == "yes":
Imperial_Weight = input("What is your weight in pounds?: ")
Imperial_Height = input("What is your height in feet (decimals)?: ")
Imperial_Weight = float(Imperial_Weight)
Imperial_Height = float(Imperial_Height)
Metric_Converted_Weight = Imperial_Weight * 2.205
Metric_Converted_Height = Imperial_Height * 30.48
Metric_Converted_Weight = str(Metric_Converted_Weight)
Metric_Converted_Height = str(Metric_Converted_Height)
print("Your metric weight is " + Metric_Converted_Weight + " kg, please note this for the next stage.")
print("Your metric height is " + Metric_Converted_Height + " cm, please note this for the next stage.")
print("--")
elif Offer_Conversions == "no":
print("--")
Age = input("Put in your age: ")
Weight = input("Put in your weight in KG: ")
Height = input("Put in your height in CM: ")
Weight = float(Weight)
Height = float(Height)
Height_Squared = Height * Height
BMI_Formula_Assisted = Weight / Height_Squared
BMI_Formula_Completed = BMI_Formula_Assisted * 10000
BMI_Formula_Completed = str(BMI_Formula_Completed)
print("You have a BMI score of " + BMI_Formula_Completed + ".")
BMI_Formula_Completed = float(BMI_Formula_Completed)
if BMI_Formula_Completed <= 18.5:
print("You are underweight, consider gaining weight to bring your BMI to between 20 and 25.")
elif BMI_Formula_Completed <= 25:
print("You are perfectly healthy, with a BMI within a healthy range of between 20 and 25.")
elif BMI_Formula_Completed <= 30:
print("You are overweight, speak to a doctor about setting yourself a new target BMI between 20 and 25.")
elif BMI_Formula_Completed >= 30.1:
print("You are obese, you need to lower your BMI to between 20 and 25 or else you may be at risks.")