2016-06-12 8 views
0
answer = input('how are you') 
if answer == 'good': 
    print('glad to hear it') 
if answer == 'what?': 
    print('how are you?') 

休憩を使用せずに、ユーザーが「何?」と入力した場合、最初からどのように再開しますか?変数とループだけでこれをどうすればできますか?ブレークを使わずにプログラムを再起動させる方法は?

+1

再帰、ループなどはすべてそれを行う方法です。 – Li357

答えて

0

これが正常に動作するはずです:

good = False 

while not good: 
    answer = input('how are you?') 
    if answer == 'what?': 
     continue 

    if answer == 'good': 
     good = True 
     print('glad to hear it') 

を変数goodTrueになると、ループが停止します。 continueはループの次の繰り返しにスキップしますが、必ずしも必要ではありません。それを残すと、'what?'が予想される入力であるという読者が示されます。

は今、あなたができれば、それは次のようになり、あなたがbreakを使用することはできません言ったが、:

while True: 
    answer = input('how are you?') 
    if answer == 'what?': 
     continue 

    if answer == 'good': 
     print('glad to hear it') 
     break 
1

「」」それは答えが.goodされるまでループし続けます。フラグが使用されていません。 '' '

answer='#' 
while(answer != 'good'): 
    answer = input('how are you\n') 
    if answer == 'good': 
     print('glad to hear it') 
0

これを達成するために複雑な作業は必要ありません。

input = '' #nothing in input 
while input != 'good': #true the first time 
    input = raw_input('how are you?') #assign user input to input 
if input == 'good': #if it's good print message 
    print('glad to hear it') 

または

input = 'what?' #what? in input 
while input == 'what?': #true the first time 
    input = raw_input('how are you?') #assign user input to input 
if input == 'good': #if it's good print message 
    print('glad to hear it') 
else: 
    print('too bad') 

最初のケースの任意の返信がwhat?除いて動作するかどうか、あなたがgood又は第二を期待している場合。

関連する問題