Skip to content

Basic calculator #57

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: master
Choose a base branch
from

Conversation

vedanshagarwal4
Copy link

No description provided.

Copy link

@ritesharrma ritesharrma left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The whole basic calculator code is fine. But I want to suggest some basic enhancement changes.
def operate(a, b, choice):
operations = {
1: a + b,
2: a - b,
3: a * b,
4: a / b if b != 0 else "Division by zero error",
5: a ** b,
6: a % b if b != 0 else "Modulo by zero error"
}

return operations.get(choice, "Invalid choice")

if name == 'main':
# Menu - Available choices
print("1. Addition\n2. Subtraction\n3. Multiplication\n4. Division\n5. Exponentiation\n6. Modulo\n")

try:
    first_num = float(input('Enter first number: '))
    second_num = float(input('Enter second number: '))
    choice = int(input('Enter choice from the above list: '))

    result = operate(first_num, second_num, choice)
    print(f"Result: {result}")
except ValueError:
    print("Invalid input. Please enter numerical values for numbers and an integer for the choice.")

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants