2017-06-10 7 views
0

私はちょうどPythonを学び始めました。Pythonでループの問題が発生する

私はPig Latin Converterのコードを書こうとしています。私はそれを書くために関数を使用しています。私はwhileループで走ります。

ブレークを使用してループを終了する方法を理解しようとしましたが、それは終了しません。また、2回目に入力するだけで動作します。

私が間違っていることを見つけたいと思っています。もしあなたが私を助けてくれたら、本当に感謝しています。 :)

pig = 'ay' 

    def func(): 
     word = input('Hello. Please Enter your First Word:').lower() 


     if len(word) > 0 and word.isalpha(): 
      first = word[0] 
      if first in ('a', 'e', 'i', 'o', 'u'): 
       print('The first letter must begin with a consonant. Would you like to try a different word? (Y/N)') and inpup().lower() 
       if input() == 'y': 
        return func() 
       else: 
        print("Thank you for using Pig Latin Converter") 
        return False 



     else: 
      new_word = word[1].upper() + word[2:] + ' ' + first.upper() + pig 
      print(new_word) 
      print("Would you like to try a different word? (Y/N)") and input().lower() 
      if input() == 'y': 
       return func() 
      else: 
       print("Thank you for using Pig Latin Converter") 
       return False 



while True: 
    func() 

    if False: 
     break 
+0

'if func():break' – abccd

答えて

1
while True: 
    func() 

    if False: 
     break 

FUNCを()真の値を返します。ありがとう、それは何にも割り当てられていません。 whileステートメントはTrueのままで、永遠にループします。真で進むように、ループが最初に実行されます。この

proceed = True 
while proceed: 
    proceed = func() 

のようなものです。この時間を、それを変更し、その後、FUNC()の戻り値に応じて)FUNCを(実行 - ボディTrueまたはFalseのどちらかをするか繰り返しまたは終了します。コードがTrueに実行される場合、再実行値を変更する必要があることに注意してください。

if input() == 'y': 
    return True 
+0

すみません、ありがとうございました。それはそれを解決しました!めっちゃ幸せ。 – dbshin310

+0

これを 'while func():pass'に簡略化することはできませんか、それともunpythonicですか? – GarethPW

+0

はい、できます。 – Rosh

-1

まず、ここでは機能のクリーン版があります。あなたはそれを呼び出すだけであなたのコンバータを実行することができます。あなたの関数が既に再帰的であるので、whileループの必要はありません。

def func(): 
    pig = 'ay' # Moved this inside as there was no need for it to be defined out of the function's scope. 
    word = input('Hello. Please Enter a Word:').lower() # Changed this so it doesn't say 'First' everytime 


    if not (len(word) > 0 and word.isalpha()): 
     # If you end up in here then the user entered something incorrectly, So we should tell them that. 
     print('The word needs to contain alphabetic characters only. Would you like to try again? (Y/N)') and inpup().lower() 
     if input() == 'y': 
      # Note that your function is recursive, so you don't need to have a while loop. 
      return func() 
     else: 
      print("Thank you for using Pig Latin Converter") 
      return False 
    first = word[0] 
    if first in ('a', 'e', 'i', 'o', 'u'): 
     print('The first letter must begin with a consonant. Would you like to try a different word? (Y/N)') and inpup().lower() 
     if input() == 'y': 
      return func() 
     else: 
      print("Thank you for using Pig Latin Converter") 
      return False 



    else: 
     # I had to replace the 'first' variable with 'word[0]'. This is because if the user entered a valid string then you would never assign the first variable. 
     new_word = word[1].upper() + word[2:] + ' ' + word[0].upper() + pig 
     print(new_word) 
     print("Would you like to try a different word? (Y/N)") and input().lower() 
     if input() == 'y': 
      return func() 
     else: 
      print("Thank you for using Pig Latin Converter") 
      return False 
func() 

これを実行すると、うまくいくはずです:-)私はどこでもコメントを付けました。

+0

変数に値を返さない場合は、Falseを返すことは冗長です。返すだけです。 – Rosh

関連する問題