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.

Edited 7 Years Ago by Dokino: n/a

okay, and what is your question?

woops forgot to add it :)
Is it possible to merge them somehow by creating something more like first method but making it infinite like second one.

I don't see the diference between them except the fact the second is better coded

and can I ask why you import __future__.division ??

I don't see the diference between them except the fact the second is better coded

and can I ask why you import __future__.division ??

hm with it all numbers are took as floats e.g without it if i would type 5.2+5.2 it would give me 10 with that it gives me 10.4
but i guess i don't need that in 2nd example hm i edited first one and i got fully working calculator
now i guess i will add sin's cos's root and other math stuff

You can put the redundant code into a function

def get_two_numbers():
    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"
    return x, y

def add():
    x, y = get_two_numbers()
    print 'your answer is:', x+y
 
def substract():
    x, y = get_two_numbers()
    print 'your answer is:', x-y

I would also suggest adding some print statements, for example:

class wtd:#wtd= what to do
    command = raw_input("What do you want to do?\nadd +\nsubstract -\ndivide /\nmultiply *\n")

cmd1 = wtd()
print "cmd1.command =", cmd1.command
bull1 = cmd1.command

Edited 7 Years Ago by woooee: n/a

This article has been dead for over six months. Start a new discussion instead.