【问题标题】:How can I repeat inputting with the for loop numbering if the answer is not accepted?如果不接受答案,如何使用 for 循环编号重复输入?
【发布时间】:2021-10-21 13:21:49
【问题描述】:

我正在使用 for 循环为我的输入答案编号。如果用户得到正确答案,它将存储在列表中。但是,如果用户想重复答案,它会打印出用户重复了答案,但问题是,它不是输入相同的数字,而是继续下一个数字

answer = []
while lives > 0:
    while repeat:
        for i in range(0,10):
            answerlst = input(str(i + 1) + '. ').upper()
            if answerlst in text:
                if answerlst in answer:
                    print("You repeated your answer!")
                    repeat = True
                else:
                    answer.append(answerlst)
                    repeat = False
            else:
                print("Incorrect. You just lose a life")
                lives -= 1
                if lives == 0:
                    print("GAME OVER!!!!!\n")
                    break

        ask = input("\nWanna play again? [Y] / [N]: ").upper()
        if ask == 'Y':
            tryAgain = True
        else:
            tryAgain = False

例子:

  1. 黑暗
  2. 黑暗 你重复了你的答案!
  3. #这是问题

不是输入数字3,而是数字2,因为它只会重复答案。

【问题讨论】:

  • 我认为您只需要先创建for 循环,其中包含while 循环。

标签: python


【解决方案1】:

在这种情况下,使用 while 循环是最好的选择。分配一个变量作为起点,如果条件为假,则递增,如果为真,则继续使用,则它将具有相同的索引。

answer = []
while lives > 0:
    while repeat:
        index = 0
        while index < 10:
            answerlst = input(str(index + 1) + '. ').upper()
            if answerlst in text:
                if answerlst in answer:
                    print("You repeated your answer!")
                    repeat = True
                    continue
                else:
                    answer.append(answerlst)
                    repeat = False
            else:
                print("Incorrect. You just lose a life")
                lives -= 1
                if lives == 0:
                    print("GAME OVER!!!!!\n")
                    break
            index += 1
        ask = input("\nWanna play again? [Y] / [N]: ").upper()
        if ask == 'Y':
            tryAgain = True
        else:
            tryAgain = False

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2021-12-20
    • 1970-01-01
    • 2022-12-04
    • 1970-01-01
    • 2015-11-05
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多