Back

A simple calculator. Answers

Q1. Get the code running.
Q2. The program asks a user for two numbers. It then asks the user what they want to do with the numbers, giving them four maths options. Upon selection, it displays the result. If an inappropriate selection is made, an appropriate message is displayed.
Q3. Meaning ful names for variables, spacing code out and indentation all help to make code easier to read.
Q4. A float is a real number, a number with a fractional part e.g. 5.76, -23.0
Q5. If you divide one number by another, you could end up with an answer that is an int but you could also get a float. E.g. 6 / 2 = 3 but 6 / 4 = 1.5
Q6. you need to capture any response that is not in the menu or you will get an error message.
Q7 - Q8. Add the following code:

elif selection == 5:
result = number1 // number2
elif selection == 6:
result = number1 % number2

Q9. Add this just before input("\n\nPress <ENTER> to quit ...")

print(
     """
Add comments about DIV and MOD here.
 
       )

Q10. 

"""A simple calculator program
This program accepts 3 numbers and then carries out a calculation
on them"""
import math

number1 = float(input("Please enter the first number >>> "))
number2 = float(input("Please enter the second number >>> "))
number3 = float(input("Please enter the third number >>> "))
print(
"""
Menu

What would you like to do with the numbers?

Press 1 to add them.
Press 2 to calculate the average.
Press 3 to display the biggest number.

"""
)

selection = int(input("Make your selection now >>> "))

if selection == 1:
     result = number1 + number2 + number3
elif selection == 2:
     result = (number1 + number2 + number3) / 3
elif selection == 3:
     if number1 > number2:
           result = number1
     else:
          result = number2
     if number3 > result:
     result = number3
else:
result = "Sorry, that's not a valid selection"

print(result)

input("\n\nPress <ENTER> to quit ...")

Back