Kyliee12 / Kyle

0 stars 1 forks source link

Calculator in python basic #1

Open Kyliee12 opened 1 month ago

Kyliee12 commented 1 month ago

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)

egeuysall commented 3 weeks ago

A Better Solution

Congrats, you wrote a great calculator app code!

The Problem

Even though your code was great there some parts may become better. For example:

The Solution

These improvements might be useful for you to thrive:

Developed Code

# 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()

Feedback

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!