Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Math program Python

I’m struggling to implement a program that prompts the user for an arithmetic expression,
(for example; 1 + 1) and then calculates and outputs the result as a floating-point value formatted to one decimal place (for example; 2.0).

Also, I want there to be a space between x and y, and a space between y and z in the equation (for example; 1 + 1, with spaces between each character).

This is basically my desired output (in a terminal):
https://i.stack.imgur.com/5LuRH.png

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

I’m pretty new to python so this will probably seem easier to more experienced coders, but anyone have any ideas, and thanks in advance.

>Solution :

You can just simply use eval directly on the expression inserted…

expression = str(input('Expression: '))
print("{:.1f}".format(eval(expression), 1))

output:

Expression: 1 + 1
2.0
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading