简体   繁体   中英

How to make it my code go back to a specific while loop in python?

I'm new to Python and I am trying to create a simple calculator. Sorry if my code is really messy and unreadable. I tried to get the calculator to do another calculation after the first calculation by trying to make the code jump back to while vi == true loop. I'm hoping it would then ask for the "Enter selection" again and then continue on with the next while loop. How do I do that or is there another way?

vi = True
ag = True
while vi == True:               #I want it to loop back to here
        op = input("Input selection: ")
        if op in ("1", "2", "3", "4"):
            vi = False
while vi == False:
     x = float(input("insert first number: "))
     y = float(input("insert Second Number: "))
     break
#Here would be an If elif statement to carry out the calculation
while ag == True:
 again = input("Another calculation? ")
 if again in ("yes", "no"):
    ag = False
 else:
     print("Please input a 'yes' or a 'no'")
if again == "no":
    print("Done, Thank you for using Calculator!")
    exit()
elif again == "yes":
    print("okay!")
    vi = True   #I want this part to loop back

Nice start. To answer your question about whether there's another way to do what you're looking for; yes, there is generally more than one way to skin a cat.

Generally, I don't use while vi==True: in one section, then follow that up with while vi==False: in another since, if I'm not in True then False is implied. If I understand your question, then basically a solution is to nest your loops, or call one loop from within the other. Also, it seems to me like you're on the brink of discovering the not keyword as well as functions.

Code

vi = True
while vi:
    print("MENU")
    print("1-Division")
    print("2-Multiplication")
    print("3-Subtraction")
    print("4-Addition")
    print("Any-Exit")
    op = input("Input Selection:")
    if op != "1":
        vi = False
    else:
        x = float(input("insert first number: "))
        y = float(input("insert Second Number: "))
        print("Placeholder operation")    
        ag = True
        while ag:
            again = input("Another calculation? ")
            if again not in ("yes", "no"):
                print("Please input a 'yes' or a 'no'")
            if again == "no":
                print("Done, Thank you for using Calculator!")
                ag = False
                vi = False
            elif again == "yes":
                print("okay!")
                ag = False

Of course, that's a lot of code to read/follow in one chunk. Here's another version that introduces functions to abstract some of the details into smaller chunks.

def calculator():
    vi = True
    while vi:
        op = mainMenu()
        if op == "\n" or op == " ":
            return None
        x, y = getInputs()
        print(x + op + y + " = " + str(eval(x + op + y)))
        vi = toContinue()
    return None

def mainMenu():
    toSelect=True
    while toSelect:
        print()
        print("\tMENU")
        print("\t/ = Division; * = Multiplication;")
        print("\t- = Subtract; + = Addition;")
        print("\t**= Power")
        print("\tSpace or Enter to Exit")
        print()
        option = input("Select from MENU: ")
        if option in "/+-%** \n":
            toSelect = False
    return option

def getInputs():
    inpt = True
    while inpt:
        x = input("insert first number: ")
        y = input("insert second number: ")
        try:
            tmp1 = float(x)
            tmp2 = float(y)
            if type(tmp1) == float and type(tmp2) == float:
                inpt = False
        except:
            print("Both inputs need to be numbers. Try again.")
    return x, y

def toContinue():
    ag = True
    while ag:
        again = input("Another calculation?: ").lower()
        if again not in ("yes","no"):
            print("Please input a 'yes' or a 'no'.")
        if again == "yes":
            print("Okay!")
            return True
        elif again == "no":
            print("Done, Thank you for using Calculator!")
            return False

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM