def n():
name = input('What is the missing animal?')
if name == 'dog':
print('Well done')
else:
print('Sorry this is not right')
rep= 0
while rep < 5:
n()
rep = rep + 1
if rep == 5:
print ('You have guessed incorrectly 5 times.)
當我運行這個並得到錯誤的答案,程序不斷重複,而不是重複最多5次。爲什麼while循環在if循環中不起作用?
任何想法?
遞歸調用從0開始。您永遠不會進入while循環的第二次迭代。 – Prune
'if'不是一個循環;這是一個分支語句。 – chepner