【问题标题】:Trying to continue asking for an input if the input entered is considered "invalid"如果输入的输入被视为“无效”,则尝试继续请求输入
【发布时间】:2019-04-03 19:07:35
【问题描述】:

这是我为 CS 课程制作的文字冒险游戏的一小部分。您正在探索一所房子,并通过告诉游戏您要向北、向南、向东还是向西来导航它。
因此,我想添加一些内容来告诉您,当您输入了无效输入时,如果您拼错了 Nroth、Suoth、Eas 或 Weast 之类的单词之一。这些只是示例,但希望您知道我的意思,只要它与北、南、东或西不完全匹配。 我将如何在这部分代码中做到这一点?

我做了一个错误示例,如果您犯了“elif room ==”porch”的拼写错误,我想输出的错误,但它应该继续询问您想去哪个方向,即使您收到该错误因为到目前为止,它会继续询问您想去哪个方向,并且无论您输入什么,它都不会根据您进入的房间输出应该说的文本。

def pickRoom(direction, room):
    if(direction == "quit") or (direction == "exit"):
        print("Better luck next time!")
        return "Exit"
    elif room == "Porch":
        if direction == "North":
            return "Pantry"
        else:
            print("That is not a valid entry!")
    elif room == "Pantry":
        if direction == "North":
            return "Kitchen"
        elif direction == "East":
            return "DiningRoom"
    elif room == "DiningRoom":
        if direction == "West":
            return "Pantry"
    elif room == "Kitchen":
        if direction == "West":
            return "LivingRoom"
        elif direction == "East":
            return "Bedroom"
    elif room ==  "Bedroom":
        if direction == "West":
            return "Kitchen"
    elif room == "LivingRoom":
        if direction == "West":
            return "Bathroom"
        elif direction == "North":
            return "Stairs"
    elif room == "Bathroom":
        if direction == "East":
            return "LivingRoom"
    elif room == "Stairs":
        if direction == "South":
            return "Bar"
    elif room == "Bar":
        if direction == "East":
            return "Shop"
    elif room == "Shop":
        if direction == "North":
            return "Closet"
        elif direction == "South":
            return "Storage"
    elif room == "Storage":
        if direction == "North":
            return "Shop"
    elif room == "Closet":
        if direction == "South":
            return "Shop"

如果您需要更大的代码部分甚至整个 .py 文件,请告诉我,谢谢。

【问题讨论】:

标签: python loops


【解决方案1】:

为了保持在显示的代码部分内,根据要求,只需在末尾添加

else:
    print("Sorry, that does not make sense to me.")
    return room

这样可以解决当前问题,即如果没有与输入匹配的编程选项,则不可预测的值将被重新调整为新的当前房间。在这种情况下,通过返回参数room,存储当前房间的变量将继续使用有效房间(当前房间)。

当返回一个不可预测的值时,它很可能不是正确的房间名称之一,它需要这样才能使逻辑结构保持正常。一旦房间变量包含垃圾,它就不能再匹配任何选项,因此不会再输出任何有意义的东西。

作为防止房间变成垃圾的额外预防措施(由于任何可能的事故之一),您可以检查房间是否是现有房间之一,否则将其重置为默认值 - 或退出并显示错误消息 “糟糕,意外传送到未知空间。下次地球见。”

【讨论】:

    【解决方案2】:

    您可以尝试针对正确选择列表进行测试。我希望这会有所帮助!

    if room in roomList:
        # availableDirection is a dictionary that 
        # has rooms as keys and valid directions as values.
        if direction in availableDirection[room]:
            # return the next room from a dictionary representing the 
            # current room where the key is direction and value is the next room.
        else:
            return "Invalid direction"
    else:
        return "Invalid room"
    

    【讨论】:

      【解决方案3】:

      我不确定您需要什么,但这可能会有所帮助:

      directions = ["south", "west", "east", "north"]
      while True:
          move = input("Choose which way you would like to go\n")
          if move.lower() in directions:
              print("You have chosen to go " + move)
          else:
              print("Invalid move!")
      

      只要有一个想法,这是一个输出:

      >>Choose which way you would like to go
      north
      >>You have chosen to go north
      >>Choose which way you would like to go
      North
      >>You have chosen to go North
      >>Choose which way you would like to go
      nothr
      >>Invalid move!
      >>Choose which way you would like to go
      

      【讨论】:

        【解决方案4】:
        elif room.lower() in ('perch','peach','pooch'):
        

        你可以只列出一份你想指出的所有拼写错误的大清单。如果他们所做的选择不正确,请检查他们输入的值是否在此列表中。

        【讨论】:

        • Levenshtein 距离模块在这里也可能有用。 pypi.org/project/python-Levenshtein 然后程序可能会问“你的意思是(Levenshtein 距离最小的世界吗?”如果距离小于,比如说,3。这将是一个使用字典的高级练习,以某种方式让计算机学习拼写错误,例如如果计算机问“这就是你的意思吗?”或“你希望我以后记住这个吗?”“est”会向东移动(Inform/ZIL 的 OOPs 语法也可能很有用,例如 EST 然后 OOPS EAST 可以将来将 EST 映射到 EAST。)
        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2020-09-27
        • 1970-01-01
        • 1970-01-01
        • 2016-11-03
        • 1970-01-01
        • 2020-04-27
        • 2017-06-15
        相关资源
        最近更新 更多