Write a program that reads an expression in a line as input and prints out the result. Only integers and operators below are allowed in the expression:
时间: 2024-02-12 19:08:01 浏览: 173
Sure, I can help you with that! Here is a Python code snippet that can evaluate simple arithmetic expressions containing only integers and the operators +, -, *, and /:
```python
expression = input("Enter an arithmetic expression: ")
result = eval(expression)
print("Result:", result)
```
You can copy and paste this code into a Python IDE or a Jupyter notebook and execute it. When prompted, enter an arithmetic expression like "2+3*4" and press Enter. The program will then evaluate the expression and print out the result, which in this case should be 14.
Note that the `eval()` function is used to evaluate the expression entered by the user as a Python expression. This means that the user can potentially enter any valid Python expression, so you should be careful when using this function in a production environment.
阅读全文