A More Advanced Calculator¶
In this exercise we will build a calculator that can do addition, subtract, multiplication, and division.
User Input¶
The user will be prompted with three things:
- The operation
- The
first_number
- The
second_number
Valid operations are:
- 'ADD'
- 'SUBTRACT'
- 'MULTIPLY'
- 'DIVIDE'
Output¶
- If the user inputs 'ADD', the progam should print the result of
first_number + second_number
. - If the user inputs 'SUBTRACT', the progam should print the result of
first_number - second_number
. - If the user inputs 'MULTIPLY', the progam should print the result of
first_number * second_number
. - If the user inputs 'DIVIDE', the progam should print the result of
first_number / second_number
. - Any other output, the program should print "Incorrect operation"
Example¶
Enter the operation: MULTIPLY
Enter the first number: 7
Enter the second number: 5
The result is: 35
Provided¶
Provided is some sample code to start that gets the user's input for the operation
as a string and the first_number
and second_number
s as integers.
In [13]:
Copied!
# Get input will return "ADD", "SUBTRACT", "MULTIPLY", or "DIVIDE"
def get_input(valid_inputs):
while True:
operation = input("Enter the operation: ")
if operation in valid_inputs:
return operation
else:
print("invalid operation")
first_number = int(input("Enter the first number: "))
second_number = int(input("Enter the second number: "))
operation = get_input(["ADD", "SUBTRACT", "DIVIDE", "MULTIPLY"])
print("do you want to continue?")
yes_no = get_input(["yes", "no"])
if yes_no == "yes":
if operation == "ADD":
result = first_number + second_number
elif operation == "SUBTRACT":
result = first_number - second_number
elif operation == "MULTIPLY":
result = first_number * second_number
elif operation == "DIVIDE":
result = first_number / second_number
print(f"The result is {result}")
# Get input will return "ADD", "SUBTRACT", "MULTIPLY", or "DIVIDE"
def get_input(valid_inputs):
while True:
operation = input("Enter the operation: ")
if operation in valid_inputs:
return operation
else:
print("invalid operation")
first_number = int(input("Enter the first number: "))
second_number = int(input("Enter the second number: "))
operation = get_input(["ADD", "SUBTRACT", "DIVIDE", "MULTIPLY"])
print("do you want to continue?")
yes_no = get_input(["yes", "no"])
if yes_no == "yes":
if operation == "ADD":
result = first_number + second_number
elif operation == "SUBTRACT":
result = first_number - second_number
elif operation == "MULTIPLY":
result = first_number * second_number
elif operation == "DIVIDE":
result = first_number / second_number
print(f"The result is {result}")
Enter the first number: 3 Enter the second number: 2 Enter the operation: ADD do you want to continue? Enter the operation: yes The result is 5
In [ ]:
Copied!
def print_max(a, b):
if a > b:
print(f"{a} is max")
elif b > a:
print(f"{b} is max")
else:
print("equal")
print_max(3, 4)
print_max(7, 4)
print_max(10, 10)
def print_max(a, b):
if a > b:
print(f"{a} is max")
elif b > a:
print(f"{b} is max")
else:
print("equal")
print_max(3, 4)
print_max(7, 4)
print_max(10, 10)
In [14]:
Copied!
from random import randint
from random import randint
In [15]:
Copied!
randint(1, 100)
randint(1, 100)
Out[15]:
68
In [16]:
Copied!
help(randint)
help(randint)
Help on method randint in module random: randint(a, b) method of random.Random instance Return random integer in range [a, b], including both end points.
In [ ]:
Copied!