-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathconditions_task.py
More file actions
25 lines (22 loc) · 860 Bytes
/
conditions_task.py
File metadata and controls
25 lines (22 loc) · 860 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
#Get user input - two numbers and an operator
num1 = input("Enter the first number: ")
num2 = input("Enter the second number: ")
operator = input("Choose the operation (+, -, /, *): ")
#if loop to check if numbers given by user are actually digits or not.
if num1.isdigit() and num2.isdigit() == True:
num1 = int(num1)
num2 = int(num2)
#if loop to print the output based on input. If operator is not valid it prints out an error.
if operator == "+":
print ("The answer is: %d" %(num1 + num2))
elif operator == "-":
print ("The answer is: %d" %(num1 - num2))
elif operator == "*":
print ("The answer is: %d" %(num1 * num2))
elif operator == "/":
print ("The answer is: %.2f" %(num1 / num2))
else:
print ("Sorry, you input an invalid operator.")
#else statement for the first if statement.
else:
print ("Sorry, you input an invalid number.")