2012-05-05 17 views
1

ユーザがexitを入力するまで、次のプログラムを続けると非常に単純なwhileループステートメントは何でしょうか?例えばPythonで単純なwhileループを繰り返す

、これまで

while response = (!'exit') 
    continue file 
else 
    break 
    print ('Thank you, good bye!') 
#I know this is completely wrong, but it's a try! 

マイファイル:あなたが設定した条件が偽になるまで

#!/usr/bin/python 
friends = {'John' : {'phone' : '0401', 
     'birthday' : '31 July', 
     'address' : 'UK', 
     'interests' : ['a', 'b', 'c']}, 
    'Harry' : {'phone' : '0402', 
     'birthday' : '2 August', 
     'address' : 'Hungary', 
     'interests' : ['d', 'e', 'f']}} 
response = raw_input("Please enter search criteria, or type 'exit' to exit the program: ").split() 
try: 
    print "%s's %s is %s" % (response[0], response[1], friends[response[0]][response[1]]) 
except KeyError: 
    print "Sorry, I don't know about that. Please try again, or type 'exit' to leave the program: " 
+0

:あなたは契約ブレーカである他のいくつかの条件があるかもしれません – user1374310

答えて

3

あなたのwhileループが継続されます。だから、あなたのコードは、ほとんどがこのループの中にいてほしい。作業が終了すると、ユーザーは「exit」と入力してエラーメッセージを印刷できます。

#!/usr/bin/python 
friends = {'John' : {'phone' : '0401', 
     'birthday' : '31 July', 
     'address' : 'UK', 
     'interests' : ['a', 'b', 'c']}, 
    'Harry' : {'phone' : '0402', 
     'birthday' : '2 August', 
     'address' : 'Hungary', 
     'interests' : ['d', 'e', 'f']}} 

response = [''] 
error_message = "Sorry, I don't know about that. Please try again, or type 'exit' to leave the program: " 

while response[0] != 'exit': 
    response = raw_input("Please enter search criteria, or type 'exit' to exit the program: ").split() 
    try: 
     print "%s's %s is %s" % (response[0], response[1], friends[response[0]][response[1]]) 
    except KeyError: 
     print error_message 
    except IndexError: 
     print error_message 

print ('Thank you, good bye!') 

このコードはあなたが望むものの出発点ですが、まだいくつかのバグがあります。ユーザーが「終了」を入力したときにエラーメッセージが表示されないように、再構成できるかどうかを確認してください。

4

continueは、引数を必要としないキーワードです。現在のループに次の繰り返しが直ちに続くことを伝えるだけです。 whileforループの内側で使用できます。

あなたのコードは、whileループ内に配置する必要があります。このコードは条件が満たされるまで継続します。条件構文が正しくありません。それはwhile response != 'exit':と読みます。条件を使用しているため、continueステートメントは必要ありません。値が"exit"でない限り、設計どおりに続きます。あなたがcontinueを利用するように望んでいた場合は、応答に他のさまざまな操作を行うつもりだったし、停止する必要がある可能性がある場合

response = '' 
# this will loop until response is not "exit" 
while response != 'exit': 
    response = raw_input("foo") 

、それが使用される可能性があります:あなたの構造は次のようになり

早めにやり直してください。 breakキーワードは、ループを実行するのと同様の方法ですが、代わりにループを完全に終了する必要があります。すなわち、私の主な質問は、「ファイルを実行し続ける」を意味し、特定の文があるかどうかである

while response != 'exit': 
    response = raw_input("foo") 

    # make various checks on the response value 
    # obviously "exit" is less than 10 chars, but these 
    # are just arbitrary examples 
    if len(response) < 10: 
     print "Must be greater than 10 characters!" 
     continue # this will try again 

    # otherwise 
    # do more stuff here 
    if response.isdigit(): 
     print "I hate numbers! Unacceptable! You are done." 
     break 
3
#!/usr/bin/python 
friends = { 
    'John' : { 
     'phone' : '0401', 
     'birthday' : '31 July', 
     'address' : 'UK', 
     'interests' : ['a', 'b', 'c'] 
    }, 
    'Harry' : { 
     'phone' : '0402', 
     'birthday' : '2 August', 
     'address' : 'Hungary', 
     'interests' : ['d', 'e', 'f'] 
    } 
} 

def main(): 
    while True: 
     res = raw_input("Please enter search criteria, or type 'exit' to exit the program: ") 
     if res=="exit": 
      break 
     else: 
      name,val = res.split() 
      if name not in friends: 
       print("I don't know anyone called {}".format(name)) 
      elif val not in friends[name]: 
       print("{} doesn't have a {}".format(name, val)) 
      else: 
       print("{}'s {} is {}".format(name, val, friends[name][val])) 

if __name__=="__main__": 
    main() 
関連する問題