ödev

ödevv
 avatar
unknown
python
9 months ago
1.3 kB
2
Indexable
def get_number(prompt):
    while True:
        try:
            return float(input(prompt))
        except ValueError:
            print("invalid input !! please enter a number")


def get_operator():
    valid_operations = ["+", "-", "*", "/"]
    while True:
        try:
            operation_choice = input("enter operation( + - * / ): ").strip()
            if operation_choice not in valid_operations:
                print("invalid operation try again")
                continue
            else:
                return operation_choice


def get_second_number(operation_choice):
    while True:
        num2 = get_number("enter the second number: ")
        if operation_choice == "/" and num2 == 0:
            print("cant divide by zero")
            continue
        else:
            return num2


def main():
    num1 = get_number("enter the first number: ")
    operation_choice = get_operator()
    num2 = get_second_number(operation_choice)

    operations ={
        "+": num1 + num2,
        "-": num1 - num2,
        "*": num1 * num2,
        "/": num1 / num2
    }

    answer = operations[operation_choice]
    print(f"{num1} {operation_choice} {num2} {answer}")


if __name__ == "__main__":
    main()





Editor is loading...
Leave a Comment