answer = input('how are you')
if answer == 'good':
print('glad to hear it')
if answer == 'what?':
print('how are you?')
休憩を使用せずに、ユーザーが「何?」と入力した場合、最初からどのように再開しますか?変数とループだけでこれをどうすればできますか?ブレークを使わずにプログラムを再起動させる方法は?
answer = input('how are you')
if answer == 'good':
print('glad to hear it')
if answer == 'what?':
print('how are you?')
休憩を使用せずに、ユーザーが「何?」と入力した場合、最初からどのように再開しますか?変数とループだけでこれをどうすればできますか?ブレークを使わずにプログラムを再起動させる方法は?
これが正常に動作するはずです:
good = False
while not good:
answer = input('how are you?')
if answer == 'what?':
continue
if answer == 'good':
good = True
print('glad to hear it')
を変数good
がTrue
になると、ループが停止します。 continue
はループの次の繰り返しにスキップしますが、必ずしも必要ではありません。それを残すと、'what?'
が予想される入力であるという読者が示されます。
は今、あなたができれば、それは次のようになり、あなたがbreak
を使用することはできません言ったが、:
while True:
answer = input('how are you?')
if answer == 'what?':
continue
if answer == 'good':
print('glad to hear it')
break
「」」それは答えが.goodされるまでループし続けます。フラグが使用されていません。 '' '
answer='#'
while(answer != 'good'):
answer = input('how are you\n')
if answer == 'good':
print('glad to hear it')
これを達成するために複雑な作業は必要ありません。
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
又は第二を期待している場合。
再帰、ループなどはすべてそれを行う方法です。 – Li357