2

Whenever I run the calculator program I created, it works fine but the text "None" keeps showing up and I don't know why. Here's the code:

def add():
    print 'choose 2 numbers to add'
    a=input('add this')
    b=input('to this')
    print a+b
    return menu()
def sub():
    print 'choose 2 numbers to subract'
    a=input('subract this')
    b=input('from this')
    print b-a
    return menu()
def menu():
    print "hello, Welcome"
    print "these are your options"
    print "1. add"
    print "2. sub"
print menu()
loop=2
def sys():
    while loop==2:
        a=input("please choose")
        if a==1:
            print add()
        elif a==2:
            print sub()
        else:
            return menu(),sys()
print sys()

Here is the output:

hello, Welcome
these are your options
1. add
2. sub
None    <-------------------------(this is what I'm talking about)
please choose

If it helps anyone here is the code for my finished calculator (it looks messed up when I past it but it works when you copy and paste)

def add():
    print 'choose 2 numbers to add'
    a=input('add this')
    b=input('to this')
    print a+b
def sub():
    print 'choose 2 numbers to subract'
    a=input('subract this')
    b=input('from this')
    print b-a
def mul():
    print 'choose 2 numbers to multiply'
    a=input("multiply this")
    b=input("by this")
    print b*a
def div():
    print 'choose what numbers your want to divide'
    a=input('divide this')
    b=input('by this')
    print a/b
def exp():
    print 'choose your number you want to exponentiate'
    a=input('multiply this')
    b=input('by the power of this')
    print a**b
def menu():
    print "hello, Welcome"
    print "these are your options"
    print "1. add"
    print "2. sub"
    print "3. mul"
    print "4. div"
    print "5. expo"
    print "0. to end"
menu()
def sys():
    while True:
        a=input("please choose")
        if a==1:
             add()
             menu()
        elif a==2:
             sub()
             menu()
        elif a==3:
             mul()
             menu()
        elif a==4:
             div()
             menu()
        elif a==5:
             exp()
             menu()
        elif a==0:
            break 
        else:
            return menu(),sys()
sys()
Jon Clements
  • 138,671
  • 33
  • 247
  • 280
user2423223
  • 115
  • 3
  • 11
  • Use dictionary in place of switch statement. [link](http://stackoverflow.com/a/60211/1097972) – iraycd May 26 '13 at 22:48

1 Answers1

9

That's because the function menu() is not returning anything, by default a function in python returns None

>>> def func():pass
>>> print func()  #use `print` only if you want to print the returned value
None

Just use:

menu() #no need of print as you're already printing inside the function body.

New version of sys() after removing return menu() from add() and sub(). Instead of using return menu() inside each function simply call the menu() function at the end of while loop itself.

def sys():
    while True:
        a = input("please choose")
        if a == 1:
            add()    # call add(), no need of print as you're printing inside add() itself
        elif a==2: 
            sub()  
        menu()       # call menu() at the end of the loop

while loop==2 actually evaluates loop==2 expression first and if it is True then the while loop continues else breaks instantly. In your case as you're not changing the value of loop variable so you can simply use while True.

>>> loop = 2
>>> loop == 2
True

Related : a basic question about "while true"

Community
  • 1
  • 1
Ashwini Chaudhary
  • 244,495
  • 58
  • 464
  • 504
  • Or more specifically, menu() returns None, so anything that returns menu() returns None. – Patashu May 26 '13 at 22:26
  • I took off "print" and just used menu(), it worked for the initial run but after it looped the "none" came up again. (im sorry if im not understanding correctly like I said im still new to this) – user2423223 May 26 '13 at 22:32
  • @user2423223 see my updated solution, by the way what's the use of that `loop == 2` condition? it is always going to be `True`. – Ashwini Chaudhary May 26 '13 at 22:37
  • @Ashwini the loop == 2 is so it keeps looping (I seen it somewhere I forget where and used it, is there another way to keep it looping?) btw thank you soooooooooooo much for that it made the calculator simpler at least to me it did – user2423223 May 26 '13 at 22:45
  • 1
    @user2423223 glad that helped, you can use `while True` to get an infinite loop. – Ashwini Chaudhary May 26 '13 at 22:46
  • @AshwiniChaudhary your help has made my program about 5 lines shorter. that loop works but can you explain it a bit on how it works. what needs to be "TRUE" for the loop to work? – user2423223 May 26 '13 at 22:53
  • @user2423223 I've added some explanation, let me know if you don't understand any part. – Ashwini Chaudhary May 26 '13 at 23:06
  • @AshwiniChaudhary I deleted the loop variables from the program completely so all there is, is While True and the program still works fine. in this case what does the loop see as "TRUE" – user2423223 May 26 '13 at 23:07
  • @user2423223 `while loop == 2` actually evaluates to `while True` in your example. So `while True` is just a more clearer way to do the same. Related : http://stackoverflow.com/a/3754632/846892, http://stackoverflow.com/a/3754685/846892 – Ashwini Chaudhary May 26 '13 at 23:11
  • @AshwiniChaudhary thank you for your help it is most appreciated. that link cleared up alot. so im off to complete my program with * and / and maybe some **. python is getting easier every day. – user2423223 May 26 '13 at 23:17