diff --git a/basics/03_control_flow/16_simple_calculator.py b/basics/03_control_flow/16_simple_calculator.py new file mode 100644 index 0000000..96b5bb8 --- /dev/null +++ b/basics/03_control_flow/16_simple_calculator.py @@ -0,0 +1,42 @@ +def add(x, y): + return x + y + +def subtract(x, y): + return x - y + +def multiply(x, y): + return x * y + +def divide(x, y): + if y == 0: + print("Error: Cannot divide by zero!") + return None + return x / y + +def simple_calculator(): + print("Select operation:") + print("1. Add") + print("2. Subtract") + print("3. Multiply") + print("4. Divide") + + choice = input("Enter choice(1/2/3/4): ") + + num1 = float(input("Enter first number: ")) + num2 = float(input("Enter second number: ")) + + if choice == '1': + print(num1, "+", num2, "=", add(num1, num2)) + elif choice == '2': + print(num1, "-", num2, "=", subtract(num1, num2)) + elif choice == '3': + print(num1, "*", num2, "=", multiply(num1, num2)) + elif choice == '4': + result = divide(num1, num2) + if result is not None: + print(num1, "/", num2, "=", result) + else: + print("Invalid Input") + +if __name__ == "__main__": + simple_calculator() diff --git a/basics/04_functions/functions.py b/basics/04_functions/functions.py new file mode 100644 index 0000000..e920203 --- /dev/null +++ b/basics/04_functions/functions.py @@ -0,0 +1,17 @@ +def add(): + a = int(input("Enter the Number: ")) + b = int(input("Enter the Number: ")) + sum = a + b + print(sum) + +def subtract(): + a = int(input("Enter first number: ")) + b = int(input("Enter second number: ")) + result = a - b + print(result) + +def multiply(): + a = int(input("Enter first number: ")) + b = int(input("Enter second number: ")) + result = a * b + print(result)