Dans mon programme, il est censé poser une question à l'utilisateur et lui donner 3 chances de deviner la bonne réponse. Mais ma boucle "while" semble donner à l'utilisateur une 4ème chance de répondre à la question et de contourner la variable "max_attempts".
print('Quiz program!\n') answer = input('What is the capital of Wisconsin? ') attempt = 1 max_attempts = 4 while answer != 'Madison': attempt += 1 print('You got it wrong, please try again.\n') answer = input('What is the capital of Wisconsin? ') if attempt == max_attempts: print('You used the maximum number of attempts, sorry. The correct answer is "Madison"') break else: print(f"Correct! Thanks for playing. It took you {attempt} attempt(s).")
5 Réponses :
Vous avez max_attempts = 4
- remplacez-le par 3.
Vous devez vérifier si le compteur de tentative tentative
est égal à max_attempts
au début de la boucle, avant de ré-incrémenter le compteur, et vous devez définir max_attempt
à 3 à la place:
print('Quiz program!\n') answer = input('What is the capital of Wisconsin? ') attempt = 1 max_attempts = 3 while answer != 'Madison': if attempt == max_attempts: print('You used the maximum number of attempts, sorry. The correct answer is "Madison"') break attempt += 1 print('You got it wrong, please try again.\n') answer = input('What is the capital of Wisconsin? ') else: print(f"Correct! Thanks for playing. It took you {attempt} attempt(s).")
Toutes les réponses ci-dessus sont correctes, il suffit d'ajouter une variante légèrement différente.
print('Quiz program!\n') attempt = 1 max_attempts = 4 while attempt < max_attempts: attempt += 1 answer = input('What is the capital of Wisconsin? ') if answer == 'Madison': print("Correct!") break else: print('You got it wrong, please try again.\n') print("Thanks for playing. It took you %s attempt(s)." %(attempt-1))
En ajustant la variable max_attempts
à 2, 3, 4, 5, vous finirez par trouver le bon nombre pour vous donner le bon comportement. Mais je crois qu'il est plus important de savoir comment penser cette question. Je suggérerais de penser en termes d'invariants de boucle: créez une condition qui est toujours vraie dans la boucle et appliquez-la pendant que vous écrivez la boucle. Dans ce cas, rendons égaux la valeur de try
et le nombre d'appels à input ()
, et voyons si votre boucle est correcte:
while answer != 'Madison': attempt += 1 print('You got it wrong, please try again.\n') answer = input('What is the capital of Wisconsin? ') if attempt == max_attempts: print('You used the maximum number of attempts, sorry. The correct answer is "Madison"') break
Le problème vient de votre état. Cela devrait être
def main(): introduction() attempt=1 while attemptValid(attempt) and answerIsWrong(askQuestion(), attempt): attempt += 1 def attemptValid(attempt): max_attempts=4 if attempt < max_attempts: return 1 print('You used the maximum number of attempts, sorry. The correct answer is "Madison"') return 0 def answerIsWrong(answer, attempt): if answer != 'Madison': return 1 print(f"Correct! Thanks for playing. It took you {attempt} attempt(s).") return 0 def introduction(): print('Quiz program!\n') def askQuestion(): return input('What is the capital of Wisconsin? ') main()
J'ai également essayé de l'implémenter d'une manière plus lisible
attempt < max_attempts:
J'ai oublié d'ajouter cette ligne après la réponse! = 'Madison' dans la fonction answerIsWrong print ('Vous vous êtes trompé, veuillez réessayer. \ N')
Est-ce que max_attempts = 3 fonctionnerait?