hello,
I've wrote this calculator
from __future__ import division
class num:
x = input ("Input first number:\n")
numb = num()
class wtd:#wtd= what to do
command = raw_input("What do you want to do?\nadd +\nsubstract -\ndivide /\nmultiply *\n")
cmd1 = wtd()
bull1 = cmd1.command
while cmd1.command != "+" and cmd1.command != "-" and cmd1.command != "/" and cmd1.command != "*":
cmd1.command = raw_input ("What do you want to do?\nadd +\nsubstract -\ndivide /\nmultiply *\n")
else:
bull1 = True
while bull1 != False:
break
else:
pass
y = input("Input second number:\n")
if cmd1.command == "+":
print numb.x + y
elif cmd1.command == "-":
print numb.x - y
elif cmd1.command == "/":
print numb.x / y
elif cmd1.command == "*":
print numb.x * y
it has no error handling and it's not infinite i mean i need to restart it every time i want to calculate something.How can i make it infinite?
And i wrote this one it has error handling, i don't need to restart it but it doesn't look like real calculator
from __future__ import division
def add():
while True:
try:
x = float(raw_input("number one\n"))
break
except ValueError:
print "Wrong input"
while True:
try:
y = float(raw_input("number two\n"))
break
except ValueError:
print "wrong input"
print 'your answer is:', x+y
def substract():
while True:
try:
x = float(raw_input("number one\n"))
break
except ValueError:
print "Wrong input"
while True:
try:
y = float(raw_input("number two\n"))
break
except ValueError:
print "wrong input"
print 'your answer is:', x-y
def divide():
while True:
try:
x = float(raw_input("number one\n"))
break
except ValueError:
print "Wrong input"
while True:
try:
y = float(raw_input("number two\n"))
break
except ValueError:
print "wrong input"
print 'your answer is:', x/y
def multiply():
while True:
try:
x = float(raw_input("number one\n"))
break
except ValueError:
print "Wrong input"
while True:
try:
y = float(raw_input("number two\n"))
break
except ValueError:
print "wrong input"
print 'your answer is:', x*y
def main():
while True:
print "(1) add"
print "(2) substract"
print "(3) divide"
print "(4) multiply"
print "(q) quit"
choice = raw_input('Enter your choice:\n')
if choice == '1':
add()
elif choice == '2':
substract()
elif choice == '3':
divide()
elif choice == '4':
multiply()
elif choice == 'q':
exit()
else:
print 'Not a correct choice:', choice
if __name__ == '__main__':
main()
My brother hes php programmer told me that i should use menus when dealing with sin's cos's etc.