Python Forum
loop until user enters no - Printable Version

+- Python Forum (https://python-forum.io)
+-- Forum: Python Coding (https://python-forum.io/forum-7.html)
+--- Forum: General Coding Help (https://python-forum.io/forum-8.html)
+--- Thread: loop until user enters no (/thread-7710.html)



loop until user enters no - JohnnyFobby - Jan-22-2018

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?


RE: Help - j.crater - Jan-22-2018

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    



RE: Help - JohnnyFobby - Jan-22-2018

(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


RE: Help - j.crater - Jan-22-2018

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.


RE: Help - JohnnyFobby - Jan-22-2018

(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?