如何停止循环?

时间:2017-11-29 10:10:00

标签: python python-3.x binary converter

我离完成二进制转换器只有一步之遥,尽管它一直在重复,它是无限循环。

def repeat1():
    if choice == 'B' or choice == 'b':
        while True:
            x = input("Go on and enter a binary value: ")
            try:
                y = int(x, 2)
            except ValueError:
                print("Please enter a binary value, a binary value only consists of 1s and 0s")
                print("")
    else:
        if len(x) > 50:
            print("The number of characters that you have entered is", len(x))
            print("Please enter a binary value within 50 characters")
            z = len(x)
            diff = z - 50
            print("Please remove", diff, "characters")
            print(" ")
        else:
            print(x, "in octal is", oct(y)[2:])
            print(x, "in decimal is", y)
            print(x, "in hexidecimal is", hex(y)[2:])
            print(" ")
            def tryagain1(): 
                print("Type '1' to convert from the same number base")
                print("Type '2' to convert from a different number base")
                print("Type '3' to stop")
                r = input("Would you like to try again? ")
                print("")
                if r == '1':
                    repeat1()
                    print("")
                elif r == '2':
                    loop()
                    print("")
                elif r == '3':
                print("Thank you for using the BraCaLdOmbayNo Calculator!")
            else:
                print("You didn't enter any of the choices! Try again!")
                tryagain1()
                print("")
                tryagain1()

我正在寻找一种方法来打破特别是在代码行上的循环" elif r ==' 3' :.我已经尝试过“打破”,但它似乎无法奏效。即使他们已经想要停止,它仍然要求用户输入二进制值。我该如何打破循环?

0 个答案:

没有答案