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

2024-04-19 03:30:49 发布

您现在位置:Python中文网/ 问答频道 /正文

我是Python新手,我正在尝试创建一个简单的计算器。很抱歉,如果我的代码真的很混乱和不可读。我试图让计算器在第一次计算之后进行另一次计算,方法是让代码跳回whilevi==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

1条回答
网友
1楼 · 发布于 2024-04-19 03:30:49

好的开始。回答你的问题,是否有其他的方法来做你正在寻找的事情;是的,给猫剥皮的方法通常不止一种

通常,我不会在一节中使用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

相关问题 更多 >