Skip to content
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

Update basic_calculator.py #361

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 12 additions & 0 deletions basic_calculator.py
Original file line number Diff line number Diff line change
@@ -1,10 +1,22 @@
def calculator():
# The try block contains code that might raise an exception.
try:
# Prompt the user to enter an expression.
expression = input("Input: ")

# Evaluate the expression using the eval() function.
# This can be a security risk if user input is not trusted.
result = eval(expression)

# Print the result of the expression.
print("Output:", result)

# The except block catches any exceptions raised in the try block.
except Exception as e:
# Print the error message.
print("Error:", e)

# The if __name__ == "__main__": block checks if the script is being run directly.
if __name__ == "__main__":
# Call the calculator function.
calculator()