Python Forum

Full Version: loop until user enters no
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
I started learning python 3 days ago and needed some help on my code...
#Basic calculator
#First python calculator
#How to improve this???
name = str(input("Hello please enter in your name: "))
print("Nice meeting you " + name)
print("This is a very basic calculator that i've made. Hope you enjoy!")


def calculator():
    choose = str(input("ADDITION(a), MULTIPLICATION(m), SUBTRACTION(s), DIVISION(d) "))
    if choose == 'a':
        number1 = int(input("Choose your first number: "))
        number2 = int(input("Choose your second number: "))
        print(number1 + number2)


    elif choose == 's':
        number1 = int(input("Choose your first number: "))
        number2 = int(input("Choose your second number: "))
        print(number1 - number2)

    elif choose == 'm':
        number1 = int(input("Choose your first number: "))
        number2 = int(input("Choose your second number: "))
        print(number1 * number2)

    elif choose == 'd':
        number1 = int(input("Choose your first number: "))
        number2 = int(input("Choose your second number: "))
        print(number1 / number2)


calculator()


go_again = str(input("Would you like to go again? Y/N "))


if go_again == 'y':
            calculator()                    #How to loop this???

elif go_again == 'n':
            print("Goodbye...")
            exit()
i want to loop the "go_again" var to where it keeps running until the user enters "no", because right now it only runs once. My guess is that it needs a while loop, but how would i do that?
while True:

    calculator()

    go_again = str(input("Would you like to go again? Y/N "))
  
    if go_again == 'y':
        continue    

    elif go_again == 'n':
        print("Goodbye...")
        break    
(Jan-22-2018, 06:53 AM)j.crater Wrote: [ -> ]
while True:

    calculator()

    go_again = str(input("Would you like to go again? Y/N "))
  
    if go_again == 'y':
        continue    

    elif go_again == 'n':
        print("Goodbye...")
        break    

Thanks bud! Pray
You are welcome, and I believe you understand how it works, since it is quite simple code.

By the way, you don't need str before input, because input will return string in any case.
(Jan-22-2018, 07:23 AM)j.crater Wrote: [ -> ]You are welcome, and I believe you understand how it works, since it is quite simple code.

By the way, you don't need str before input, because input will return string in any case.

yes i understand it. Also thanks for the tip!

(Jan-22-2018, 07:23 AM)j.crater Wrote: [ -> ]You are welcome, and I believe you understand how it works, since it is quite simple code.

By the way, you don't need str before input, because input will return string in any case.

Do you have any other tips for me?