Algorithms and Data Structures with PythonChapter 56
6. Incorporating Advanced Functions
Section 6 of 8-~ 12 min read-Synced from Cuantum content
Now that we've defined our advanced functions, let's add them to our main loop.
# ... previous code ... print("Options:")print("Enter 'add' for addition")print("Enter 'subtract' for subtraction")print("Enter 'multiply' for multiplication")print("Enter 'divide' for division")print("Enter 'power' for raising to a power")print("Enter 'square_root' for square root")print("Enter 'factorial' for factorial")print("Enter 'quit' to end the program") # ... previous code ... if user_input == 'power': x = float(input("Enter the base number: ")) y = float(input("Enter the power: ")) print(f"{x} raised to the power of {y} is {power(x, y)}") elif user_input == 'square_root': x = float(input("Enter the number: ")) print(f"The square root of {x} is {square_root(x)}") elif user_input == 'factorial': x = int(input("Enter the number: ")) if x < 0: print("Factorial is not defined for negative numbers!") else: print(f"The factorial of {x} is {factorial(x)}") # ... rest of the code ...