首页 > 解决方案 > 必须多次输入负数才能跳出循环

问题描述

这是我编写的一段代码,除了 else 分支外,它按预期工作。我必须在没有 y 的情况下输入 3 次才能成功进入该分支。有谁明白为什么会这样?

def marker_question(position, marker_position):
    invalid_input = True
    while invalid_input:
        write("Would you like to do anything with the marker?")
        answer = input().lower().strip()
        if "y" in answer:
            write("Would you like to pick it up and place it or would you like to teleport to it?")
            answer = input().lower().strip()
            if "pick" in answer or "place" in answer:
              marker_position = position
              invalid_input = False
            elif "teleport" in answer:
              position = marker_position
              invalid_input = False
            else:
              write("That is not a possible action")
        else:
            write("You have decided to do nothing with the marker")
            invalid_input = False
    return position, marker_position

这是输入/输出的示例图像 测试

标签: pythonpython-3.x

解决方案


我不确定是什么问题,但尝试用引号输入它对我有用 在此处输入图像描述

检查输入功能是否正确使用


推荐阅读