Skip to content

Instantly share code, notes, and snippets.

@bitwiser
Last active August 29, 2015 13:56
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save bitwiser/9136126 to your computer and use it in GitHub Desktop.
Save bitwiser/9136126 to your computer and use it in GitHub Desktop.
PLUS = ' + '
MINUS = ' - '
MULTIPLY = ' * '
DIVIDE = ' / '
if __name__=='__main__':
print('Calculator program (\'q\' or \'quit\' to exit):\n')
while True:
s = raw_input('Enter operation: ')
if s=='q' or s=='quit':
print('Bye!!!')
exit()
if s.find(PLUS)>=0:
l = s.find(PLUS)
a = float(s[:l])
b = float(s[l+3:])
print('Adding {0} and {1}: {2}'.format(a,b,a+b))
elif s.find(MINUS)>=0:
l = s.find(MINUS)
a = float(s[:l])
b = float(s[l+3:])
print('Difference of {0} and {1}: {2}'.format(a,b,a-b))
elif s.find(MULTIPLY)>=0:
l = s.find(MULTIPLY)
a = float(s[:l])
b = float(s[l+3:])
print('Multiplying {0} and {1}: {2}'.format(a,b,a*b))
elif s.find(DIVIDE)>=0:
l = s.find(DIVIDE)
a = float(s[:l])
b = float(s[l+3:])
if b==0.0:
print('Cannot Divide by Zero.')
continue
print('Dividing {0} and {1}: {2}'.format(a,b,a/b))
else:
print('Illegal input!!!')
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment