寻求答案时,while循环超过最大尝试次数(3)

时间:2019-02-17 04:48:44

标签: python loops while-loop

在我的程序中,应该向用户提问,并给他们3次机会来猜测正确的答案。但是我的“ while”循环似乎为用户提供了第四次回答问题的机会,并绕过了“ 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).")

6 个答案:

答案 0 :(得分:1)

您有Original String = "Hello" First index at: 0 Next index at: 6 Substring = Hello -将其更改为3。

答案 1 :(得分:1)

在循环开始之前,应检查计数器尝试attempt是否等于max_attempts,然后再递增计数器,而应将max_attempt设置为3:

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).")

答案 2 :(得分:1)

以上所有答案都是正确的,只是添加了一个稍有不同的变体。

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))

答案 3 :(得分:0)

当然,通过将变量max_attempts调整为2、3、4、5,您最终会找到正确的数字来提供正确的行为。但是我相信知道如何思考这个问题更为重要。我建议考虑循环不变式:构成一个在循环中始终为真的条件,并在编写循环时强制执行。在这种情况下,让我们将attempt的值和input()的调用次数相等,看看循环是否正确:

print('Quiz program!\n')
answer = input('What is the capital of Wisconsin? ')
attempt = 1
max_attempts = 4

因此,您的attemptinput()之后设置为1。这部分还可以,并且满足不变性(即使它在循环之前)。然后循环:

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

您增加attempt,然后打印,然后呼叫input()。我将在attempt调用后紧跟input()行,以与上面的代码保持一致,但是无论哪种情况,在if语句之前,我们仍然具有attempt的值等于我们称为input()的次数。这就是为什么您有这种行为。

现在介绍如何更改代码:现在,您知道循环中的“不变式”。您必须决定(1)何时进行attempt == max_attempts的检查,以及(2)要检查的max_attempts的正确值是多少。别人的回答已经为您提供了解决方案。

答案 4 :(得分:0)

问题在于您的状况。应该是

attempt < max_attempts:

我还尝试以一种更具可读性的方式实现它

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()

答案 5 :(得分:0)

非常感谢你们,已经查看了评论并决定发布此评论,因为他们提供了很多帮助,并且对每个人的回复都有些麻烦。