Python Forum
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
loop until user enters no
#1
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?

User has been warned for this post. Reason: bad title description
Reply
#2
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    
Reply
#3
(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
Reply
#4
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.
Reply
#5
(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?
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  WHILE LOOP NOT RETURNING USER INPUT AFTER ZerroDivisionError! HELP! ayodele_martins1 7 990 Oct-01-2023, 07:36 PM
Last Post: ayodele_martins1
  Multiple user defined plots with secondary axes using for loop maltp 1 1,393 Apr-30-2022, 10:19 AM
Last Post: maltp
  WHILE Loop - constant variables NOT working with user input boundaries C0D3R 4 1,434 Apr-05-2022, 06:18 AM
Last Post: C0D3R
  Looking for a way to loop until user enters from a list? PythonW19 7 3,266 Mar-21-2021, 08:56 PM
Last Post: PythonW19
  Loop back through loop based on user input, keeping previous changes loop made? hbkpancakes 2 2,884 Nov-21-2020, 02:35 AM
Last Post: hbkpancakes
  Nested Loop for user input Ads 2 3,525 Dec-30-2019, 11:44 AM
Last Post: Ads
  Server infinite loop input from user tomislav91 1 4,169 May-23-2019, 02:18 PM
Last Post: heiner55
  Read from a file, get user input in a while loop lynden 8 4,057 Nov-05-2018, 12:31 PM
Last Post: lynden
  [Help] sorted() in while loop with user's input() {Screenshot attached} vanicci 5 3,957 Aug-04-2018, 08:59 PM
Last Post: vanicci

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020