Open Kyliee12 opened 1 month ago
Congrats, you wrote a great calculator app code!
Even though your code was great there some parts may become better. For example:
These improvements might be useful for you to thrive:
# Define the main function
def main():
num1 = float(input("Enter first number: "))
operator = input("Enter operator (+, -, *, /): ")
num2 = float(input("Enter second number: "))
enter_number()
# Define the operations function
def enter_number():
try:
num1 = float(input("Enter first number: "))
operator = input("Enter operator (+, -, *, /): ")
num2 = float(input("Enter second number: "))
if operator == '+':
print(f"Result: {num1 + num2}")
elif operator == '-':
print(f"Result: {num1 - num2}")
elif operator == '*':
print(f"Result: {num1 * num2}")
elif operator == '/':
if num2 != 0:
print(f"Result: {num1 / num2}")
else:
print("Error: Division by zero is not allowed.")
else:
print("Invalid operator. Please enter +, -, *, or /.")
except ValueError:
print("Invalid input. Please enter numeric values.")
# Call the function if the program is runned inside the program
if __name__ == "__main__":
main()
I want to mention that I’m an intermediate programmer and I might have some problems but I try my best to help Python learners because I also learned the hard ways and I want such people to learn in the proper way. I hope you success in your calculator app!
CALCULATOR IN PYTHON
num1 = int(input("Enter number: ")) od = input("Operators: ") num2 = int(input("Enter number: "))
if od == '+': print(num1 + num2) elif od == '-': print(num1-num2) elif od == '': print(num1num2) elif od == '/': print(num1/num2)