View Single Post
  #5 (permalink)  
Old 03-31-2008, 07:18 PM
Uninverted Uninverted is offline
Newbie
 
Join Date: Dec 2007
Posts: 11
Credits: 0
Rep Power: 0
Uninverted is on a distinguished road
Default Re: How to implement a simple calculator?

Quote:
Originally Posted by zorocke View Post
I am wondering what your code looks like because I am still very unfamiliar with Python. Do you think you could post those 10 lines?
Sure, always glad to help a new pythonist.

Code:
#!/usr/bin/python
import re #Import the regular expression module

pat = re.compile('\d+ *[+\-*\/] *\d+') #Set up the Regex pattern for a math problem
prob = raw_input() #Get the problem

if pat.match(prob):
    print eval(prob) #Print the evaluated problem, but only if it matches the pattern
else:
    print "Invalid math problem" #If it doesn't, then it isn't a math problem
But if you reduce it to the absolute minimum, you can do it in one line!

Code:
eval(raw_input())
__________________
My other car return the first item in a list.

Last edited by Uninverted; 03-31-2008 at 07:35 PM.
Reply With Quote

Sponsored Links