So im extremely new to python. Im teaching it to myself for a school project and i heard that calculators are a good place to start so that's exactly what im doing. I have only been using it for about a day now so like i said im pretty newb. So i have a menu that you choose a certain calculation from such as addition, subtraction, etc. From there you choose how many number you want to add, subtract, etc. Then it gets stuck inside the while loop when i want it to go back to the beginning where the menu is so you can pick a new function without having to restart the program. Here's my code, sorry i know its kinda long:

ans=True
while ans:
    print ("""
Please pick one of the following calculations:

    (1) Addition
    (2) Subtraction
    (3) Multiplication
    (4) Division
    (5) Exit/Quit
    """)
    ans=raw_input("What would you like to do? ")
    #addition
    if ans=="1":
        ans=raw_input("""
Thanks for choosing addition mode!  
How many numbers would you like to add? """)
        while ans:
            if ans=="2":
                a = raw_input('enter your first number: ')
                a = int(a)

                b = raw_input('enter your second number: ')
                b = int(b)

                total = a+b
                print 'The sum of those numbers is', total
            if ans=="3":
                a = raw_input('enter your first number: ')
                a = int(a)

                b = raw_input('enter your second number: ')
                b = int(b)

                c = raw_input('enter your third number: ')
                c = int(c)

                total = a+b+c
                print 'The sum of those numbers is', total
            if ans=="4":
                a = raw_input('enter your first number: ')
                a = int(a)

                b = raw_input('enter your second number: ')
                b = int(b)

                c = raw_input('enter your third number: ')
                c = int(c)

                d = raw_input('enter your fourth number: ')
                d = int(d)

                total = a+b+c+d
                print 'The sum of those numbers is', total
            if ans=="5":
                a = raw_input('enter your first number: ')
                a = int(a)

                b = raw_input('enter your second number: ')
                b = int(b)

                c = raw_input('enter your third number: ')
                c = int(c)

                d = raw_input('enter your fourth number: ')
                d = int(d)

                e = raw_input('enter your fifth number: ')
                e = int(e)

                total = a+b+c+d+e
                print 'The sum of those numbers is', total
            else:
                print "Los Siento! Invalid input."
                break
    #subtraction            
    if ans=="2" :       
        ans=raw_input("""
Thanks for choosing subtraction mode!
How many numbers would you like to subtract? """)
        while ans:
            if ans=="2":
                a = raw_input('enter your first number: ')
                a = int(a)

                b = raw_input('enter your second number: ')
                b = int(b)

                total = a-b
                print 'The difference between those numbers is', total
            if ans=="3":
                a = raw_input('enter your first number: ')
                a = int(a)

                b = raw_input('enter your second number: ')
                b = int(b)

                c = raw_input('enter your third number: ')
                c = int(c)

                total = a-b-c
                print 'The difference between those numbers is', total
            if ans=="4":
                a = raw_input('enter your first number: ')
                a = int(a)

                b = raw_input('enter your second number: ')
                b = int(b)

                c = raw_input('enter your third number: ')
                c = int(c)

                d = raw_input('enter your fourth number: ')
                d = int(d)

                total = a-b-c-d
                print 'The difference between those numbers is', total
            if ans=="5":
                a = raw_input('enter your first number: ')
                a = int(a)

                b = raw_input('enter your second number: ')
                b = int(b)

                c = raw_input('enter your third number: ')
                c = int(c)

                d = raw_input('enter your fourth number: ')
                d = int(d)

                e = raw_input('enter your fifth number: ')
                e = int(e)

                total = a-b-c-d-e
                print 'The difference between those numbers is', total
    #Multiplication         
    if ans=="3":    
        print "Thanks for choosing multiplication mode!"

        a = raw_input('enter your first number: ')
        a = int(a)

        b = raw_input('enter your second number: ')
        b = int(b)

        total = a*b
        print 'The product of those two number is', total       
    #division   
    if ans=="4":    
        print "Thanks for choosing division mode!"

        a = raw_input('enter your first number: ')
        a = int(a)

        b = raw_input('enter your second number: ')
        b = int(b)

        total = a/b
        print 'The qoutient of those two number is', total
    #Exit/Quit
    if ans=="5":
        print "Goodbye!"
        break
    else:
        print "Los Siento! Invalid input."

Instead of

if ...
if ...
if ...
else ...

use

if ...
elif ...
elif ...
else ...

If you need to go to the next iteration of a while loop, you can use a

continue

statement.

Edited 2 Years Ago by Gribouillis

Thanks @Gribouillis that worked perfectly! Whats the purpose of using elif instead of if? Whats the difference?

Suppose you have

if ans == "2":
    print("foo")
if ans == "3":
    print("bar")
else:
    print("qux")

and ans is the string "2". Foo is printed in the first if, but then the program tries the test ans == "3" and as it fails, the else part is executed, printing qux. Here the else part applies to if ans == "3" only.

With elif, "foo" is printed, but the second test is not tested, and the else part is not executed. The else part would be executed only if none of the if, elif ... cases applies.

Edited 2 Years Ago by Gribouillis

Makes sense, although now i have another similar problem. When i enter a certain mode, say addition for example, i will choose the amount of numbers i want to add, add them together, and it will give me the answer. All well, but after that it keeps asking for my first, second, and third number and gives me the answer for those. After it gives me the sum of all of the numbers i want it to go back to the menu again but its not doing that. Here's my updated code:

ans=True
while ans:
    print ("""
Please pick one of the following calculations:

    (1) Addition
    (2) Subtraction
    (3) Multiplication
    (4) Division
    (5) Exit/Quit
    """)
    ans=raw_input("What would you like to do? ")
    #addition
    if ans=="1":
        ans=raw_input("""
Thanks for choosing addition mode!  
How many numbers would you like to add? """)
        while ans:
            if ans=="2":
                a = raw_input('enter your first number: ')
                a = int(a)

                b = raw_input('enter your second number: ')
                b = int(b)

                total = a+b
                print 'The sum of those numbers is', total
            elif ans=="3":
                a = raw_input('enter your first number: ')
                a = int(a)

                b = raw_input('enter your second number: ')
                b = int(b)

                c = raw_input('enter your third number: ')
                c = int(c)

                total = a+b+c
                print 'The sum of those numbers is', total
            elif ans=="4":
                a = raw_input('enter your first number: ')
                a = int(a)

                b = raw_input('enter your second number: ')
                b = int(b)

                c = raw_input('enter your third number: ')
                c = int(c)

                d = raw_input('enter your fourth number: ')
                d = int(d)

                total = a+b+c+d
                print 'The sum of those numbers is', total
            elif ans=="5":
                a = raw_input('enter your first number: ')
                a = int(a)

                b = raw_input('enter your second number: ')
                b = int(b)

                c = raw_input('enter your third number: ')
                c = int(c)

                d = raw_input('enter your fourth number: ')
                d = int(d)

                e = raw_input('enter your fifth number: ')
                e = int(e)

                total = a+b+c+d+e
                print 'The sum of those numbers is', total
            else:
                print "Los Siento! Invalid input."
                break
    #subtraction            
    elif ans=="2"   :       
        ans=raw_input("""
Thanks for choosing subtraction mode!
How many numbers would you like to subtract? """)
        while ans:
            if ans=="2":
                a = raw_input('enter your first number: ')
                a = int(a)

                b = raw_input('enter your second number: ')
                b = int(b)

                total = a-b
                print 'The difference between those numbers is', total
            elif ans=="3":
                a = raw_input('enter your first number: ')
                a = int(a)

                b = raw_input('enter your second number: ')
                b = int(b)

                c = raw_input('enter your third number: ')
                c = int(c)

                total = a-b-c
                print 'The difference between those numbers is', total
            elif ans=="4":
                a = raw_input('enter your first number: ')
                a = int(a)

                b = raw_input('enter your second number: ')
                b = int(b)

                c = raw_input('enter your third number: ')
                c = int(c)

                d = raw_input('enter your fourth number: ')
                d = int(d)

                total = a-b-c-d
                print 'The difference between those numbers is', total
            elif ans=="5":
                a = raw_input('enter your first number: ')
                a = int(a)

                b = raw_input('enter your second number: ')
                b = int(b)

                c = raw_input('enter your third number: ')
                c = int(c)

                d = raw_input('enter your fourth number: ')
                d = int(d)

                e = raw_input('enter your fifth number: ')
                e = int(e)

                total = a-b-c-d-e
                print 'The difference between those numbers is', total
            else:
                print "Los Siento! Invalid input."
                break
    #Multiplication         
    elif ans=="3":  
        ans=raw_input("""
Thanks for choosing multiplication mode!
How many numbers would you like to multiply? """)
        while ans:
            if ans=="2":
                a = raw_input('enter your first number: ')
                a = int(a)

                b = raw_input('enter your second number: ')
                b = int(b)

                total = a*b
                print 'The product of those numbers is', total
            elif ans=="3":
                a = raw_input('enter your first number: ')
                a = int(a)

                b = raw_input('enter your second number: ')
                b = int(b)

                c = raw_input('enter your third number: ')
                c = int(c)

                total = a*b*c
                print 'The product of those numbers is', total
            elif ans=="4":
                a = raw_input('enter your first number: ')
                a = int(a)

                b = raw_input('enter your second number: ')
                b = int(b)

                c = raw_input('enter your third number: ')
                c = int(c)

                d = raw_input('enter your fourth number: ')
                d = int(d)

                total = a*b*c*d
                print 'The product of those numbers is', total
            elif ans=="5":
                a = raw_input('enter your first number: ')
                a = int(a)

                b = raw_input('enter your second number: ')
                b = int(b)

                c = raw_input('enter your third number: ')
                c = int(c)

                d = raw_input('enter your fourth number: ')
                d = int(d)

                e = raw_input('enter your fifth number: ')
                e = int(e)

                total = a*b*c*d*e
                print 'The product of those numbers is', total
            else:
                print "Los Siento! Invalid input."
                break
    #division   
    elif ans=="4":  
        ans=raw_input("""
Thanks for choosing division mode!
How many numbers would you like to divide? """)
        while ans:
            if ans=="2":
                a = raw_input('enter your first number: ')
                a = int(a)

                b = raw_input('enter your second number: ')
                b = int(b)

                total = a/b
                print 'The qoutient of those numbers is', total
            elif ans=="3":
                a = raw_input('enter your first number: ')
                a = int(a)

                b = raw_input('enter your second number: ')
                b = int(b)

                c = raw_input('enter your third number: ')
                c = int(c)

                total = a/b/c
                print 'The quotient of those numbers is', total
            elif ans=="4":
                a = raw_input('enter your first number: ')
                a = int(a)

                b = raw_input('enter your second number: ')
                b = int(b)

                c = raw_input('enter your third number: ')
                c = int(c)

                d = raw_input('enter your fourth number: ')
                d = int(d)

                total = a/b/c/d
                print 'The qoutient of those numbers is', total
            elif ans=="5":
                a = raw_input('enter your first number: ')
                a = int(a)

                b = raw_input('enter your second number: ')
                b = int(b)

                c = raw_input('enter your third number: ')
                c = int(c)

                d = raw_input('enter your fourth number: ')
                d = int(d)

                e = raw_input('enter your fifth number: ')
                e = int(e)

                total = a/b/c/d/e
                print 'The quotient of those numbers is', total
            else:
                print "Los Siento! Invalid input."
                break
    #Exit/Quit
    elif ans=="5":
        print "Goodbye!"
        break
    else:
        print "Los Siento! Invalid input."

Edited 2 Years Ago by cheeseman125

I think the most inner while ans: serve no purpose. You could remove them. On the other hand, I would replace the while ans at line 2 by while True (which means loop indefinitely), because the variable ans takes different meanings at different places, so you don't really control its value.

The correct way to write this code is to use functions, but perhaps you didn't learn what functions are ?

Edited 2 Years Ago by Gribouillis

"ans" is never changed under the "while ans" loop so it loops continuously. A function Click Here would make things easier and cleaner

def get_numbers(number_to_get):
    return_list = []
    lits = ["first", "second", "third", "fourth", "fifth"]
    for num in range(number_to_get):
        input_num = raw_input("enter your %s number " %(lits[num] ))
        return_list.append(input_num)
    return return_list

ans=raw_input("""
Thanks for choosing addition mode!  
How many numbers would you like to add? """)
numbers_list = get_numbers(int(ans))
print numbers_list

Edited 2 Years Ago by Gribouillis: fixed indention

no i never did learn exactly what functions are, how they work, and how to use them. Im pretty much just trying to teach myself so theres not really a method to my madness. Thanks for the help and i'll look into fuctions.

I know that you are still learning, but you may want to get into the habit of using more descriptive variables. Doing so will make your life much easier later when debugging or months from now when you are trying to figure out how something you wrote works. I know, cause it happened to me--"What the hell is "b," and where did "c" come from?"

As for functions, you should learn about, and how to use those as soon as possible. As you write your code, try to identify code that is repeated, and then move that code into a function. You will like it Sam I Am.

Good luck and happy coding.

Edited 1 Year Ago by BustACode: Add a comma.

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