繁体   English   中英

如何使我的代码 go 回到 python 中的特定 while 循环?

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

我是 Python 的新手,我正在尝试创建一个简单的计算器。 抱歉,如果我的代码真的很混乱且难以阅读。 我试图让计算器在第一次计算后进行另一次计算,方法是让代码跳回 while vi == true 循环。 我希望它会再次要求“输入选择”,然后继续下一个 while 循环。 我该怎么做或有其他方法吗?

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

好的开始。 回答您关于是否有另一种方法可以做您正在寻找的事情的问题; 是的,通常有不止一种方法可以给猫剥皮。

一般来说,我不会在一个部分中使用while vi==True: ,然后在另一个部分中使用while vi==False: ,因为如果我不在True中,则暗示False 如果我理解你的问题,那么基本上一个解决方案是嵌套你的循环,或者从另一个循环中调用一个循环。 此外,在我看来,您似乎正处于发现not关键字和函数的边缘。

代码

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

当然,一大堆代码需要阅读/遵循。 这是另一个版本,它引入了将一些细节抽象成更小的块的函数。

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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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