2017-11-29 10 views
0

私はバイナリコンバータを完成させるために少し離れています。ループを止めるには?

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

(これはPythonと仮定します)なぜ、別の関数内で 'tryagain1'関数を定義していますか? – svgrafov

+0

それは実際にはPythonです、私はPython 3を使用しています。とにかく、私はユーザーがより簡単な時間を持つためにそれを行いました。私が問題の根源は、「while True」をfalseにする方法だと思う。 –

+0

あなたのコードはここに示されているようにインデントされていますか? – svgrafov

答えて

1
  elif r == '3': 
       print("Thank you for using the BraCaLdOmbayNo Calculator!") 
       return 0 

     else: 
      print("You didn't enter any of the choices! Try again!") 
      choice = try again() 
      if choice == 0: 
       return 0 
      print("") 
      tryagain1() 

returnとき機能やコードの末尾に使用することが想定されます

関連する問題