hello,
I'm writing calculator with Python and i came across few problems.
First problem is that if user enters letters it would ask him to reenter them i thought i could do it like this

try:
    class num:
        x = float(raw_input("number\n"))
    numb = num()
except:
    bull = False
else:
    bull = True
if bull == False:
    import vartype2
else:
    print "aha"

but import statement imports it only once and i have no clues how to fix it.
Second problem is that when if i want to continue counting i need to restart the program all over again i tried something like this

dothis = raw_input("Do you want to continue?\n")

while dothis != "y" and dothis != "ye" and dothis != "yes":
    print raw_input("Press Enter to exit\n")
    break
else:
     import calc1

but once again import statement imports it only once.
How could i fix these bugs?

but import statement imports it only once and i have no clues how to fix it.

Why do want to import the same thing a second time? You already have it in memory. Putting the same thing in another block of memory is a waste.

To test for a number, there are several ways to do it. The simplest to understand is to test each character of the input for >= "0" and <= "9". Take a look at the first part here for iterating over a string. http://www.pasteur.fr/formation/infobio/python/ch11s02.html

Edited 7 Years Ago by woooee: n/a

Well, maybe you should redesign your program? It looks odd.

why does it look odd?
whole script looks like this

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

dothis = raw_input("Do you want to continue?\n")

while dothis != "y" and dothis != "ye" and dothis != "yes":
    print raw_input("Press Enter to exit\n")
    break
else:

Edited 7 Years Ago by Dokino: n/a

why does it look odd?

Two classes with 1 attributt(variable) in each,is not a normal OO design.
Your program work,but for me this is not a good way to code.

Some problem.
Menu should come first so user now what this is about.
You have no exception handling,so if you type a letter program crash.

Edited 7 Years Ago by snippsat: n/a

Two classes with 1 attributt(variable) in each,is not a normal OO design.
Your program work,but for me this is not a good way to code.

Some problem.
Menu should come first so user now what this is about.
You have no exception handling,so if you type a letter program crash.

hmm error handling um could you show me e.g how to code that?

Look at this here two way to work with function.
Maybe you get some idèe on how to solve your +*-/ with function and a menu.
One function i just return and do calulation in main.
They other function all calulation and exception handling is done in fuction.

Try to get comfortable with use off function.
Then look at classes and a more OOP apropose to solve problem. except: is the easy way here a catch many errors.
You can catch error you need.
Try option 1 the one without exception handling and type a letter.
Now you get a NameError
To catch that except NameError: Just a litte on how to handle error.

def radius_1(r):
    '''Here we just return and do calulation in main'''
    pi = 3.14
    return pi * r * r

def radius_2():
    '''
    Here we do calulation in fuction,with exception handling
    Try to breake it with wrong input,you see that not so easy
    '''    
    while True:
        try:
            r = input ('enter radius: ')
            pi = 3.14
            radius_sum = pi * r * r
            if r >=0:
              print 'The radius is %.2f' % (radius_sum)
              break  #We break out off loop when answer is correct and back to menu
            else:
                print 'Invalid radius'
        except:
            print 'Wrong input'    

def main():
    '''Main menu and info ''' 
    while True:               
        print 'Welcome to my menu\n'
        print '(1) Calculate radius example_1'
        print '(2) Calculate radius example_2' 
        print '(q) Quit' 
        choice = raw_input('Enter your choice: ') 
        if choice == '1':
            #We to input and caluation here,no exception handling as an example(try a letter or -5)            
            r = input (' enter radius: ')
            print 'Radius is %.2f' % (radius_1(r))
        elif choice == '2': 
            radius_2() #We call radius_2 and all calculation is done in function
        elif choice == 'q': 
            exit() 
        else: 
            print 'Not a correct choice:', choice   

if __name__ == '__main__':
    main()

Edited 7 Years Ago by snippsat: n/a

and how do i take information from module?
i've thought of this

def x():
    while True:
        try:
            x = float(raw_input("Input first number"))
            break
        except:
            print "Input your first number"

def y():
    while True:
        try:
            y = float(raw_input("Input second number"))
            break
        except:
            print "Input your first number"


        
def main():
    '''main meniu and info'''

    print '(+) to add'
    print '(-) to substract'
    print '(/) to divide'
    print '(*) to multiply'
    print '(q) to quit'
    choise = raw_input("enter your comand")
    if choise == '+':
        print #x from module x + y from module y



if __name__ == '__main__':
    main()

how do i get that x and y

Is it not better to make one function like this to add 2 number.

def add_number():
    n = raw_input('Enter number to add format (2+2)')
    num1, num2 = n.split('+')
    my_sum = int(num1) + int(num2)
    print my_sum

add_number()

You should not write except: clauses without targeting a specific exeption. For example if you're reading user input and you're expecting a number and the user types "hello", then float("hello") will raise a ValueError. You can catch it with except ValueError .
There are many good reasons not to use except: alone. One of them is that when there is a bug in a python program, python will raise an unexpected exception. This exception will not be caught and you will have a nice tracebak in the output, which tells you where is your error. If you catch all exceptions, you will not see the traceback, and you will waste time finding the bug. In python, NOT catching exceptions is a powerful debugging tool. There are other reasons too.

Edited 7 Years Ago by Gribouillis: n/a

Is it not better to make one function like this to add 2 number.

def add_number():
    n = raw_input('Enter number to add format (2+2)')
    num1, num2 = n.split('+')
    my_sum = int(num1) + int(num2)
    print my_sum

add_number()

well i don't like the format how you enter numbers.
the way i want it to be is like you type in first number then you type in the command then you type in 2nd number
i mean if i would chose method you offer first i chose what i want to do e.g add divide etc. and then i type in numbers it makes no sense :)

This question has already been answered. Start a new discussion instead.