0

This is a small section from a text adventure game I am making for a CS class. You are exploring a house and you navigate it by telling the game if you want to go north, south, east or west.
So I wanted to add something to tell you when you have entered an invalid input, if you say misspelled one of the words like Nroth, Suoth, Eas, or Weast. These are just examples but hopefully you know what I mean, just if it doesn't match exactly north, south, east or west. How would I do that within this section of code?

I made an example of the error I want to to output if you made a spelling mistake where it says "elif room == "porch" but it should continue asking which direction you want to go, even if you get that error because as of now it continues asking which direction you want to go and no matter what you put in, it does not output the text that is supposed to be said depending on which room you enter.

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"

Let me know if you need a larger section of the code or even the whole .py file to figure it out, thanks.

Yunnosch
  • 26,130
  • 9
  • 42
  • 54
Apex MC
  • 23
  • 2
  • Related reading: [Asking the user for input until they give a valid response](https://stackoverflow.com/q/23294658/953482) – Kevin Oct 30 '18 at 17:03
  • Please cresate an [mcve] which includes the loop from which the shown function is called. – Yunnosch Oct 30 '18 at 17:06

4 Answers4

0
elif room.lower() in ('perch','peach','pooch'):

You could just have one big list of all the misspellings that you want to point out. If the choice(s) they have made is incorrect, check if the value they have entered is in this list.

Andy G
  • 19,232
  • 5
  • 47
  • 69
  • The Levenshtein distance module may be useful here, too. https://pypi.org/project/python-Levenshtein/ The program could then ask "did you mean (the world with the smallest Levenshtein distance?" if the distance is less than, say, 3. It would be an advanced exercise with dictionaries to somehow allow the computer to learn typos e.g. "est" would go to east, if the computer asked "Is this what you meant?" or "Would you like me to remember this in the future?" (Inform/ZIL's OOPs syntax could also be useful e.g. EST then OOPS EAST could map EST to EAST in the future.) – aschultz Aug 02 '19 at 09:23
0

I am not sure what do you need, but this might help:

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

To just having an idea, this is a output:

>>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
Michael Tesař
  • 342
  • 5
  • 15
0

You could try testing against a list of correct choices. I hope this helps!

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"
Andrew
  • 43
  • 7
0

In order to stay within the shown code part, as requested, just add at the end

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

This way you fix the current problem, that in case of none of the programmed options matching the input a non-predictable value will be retuned as the new current room. By returning the parameter room in that case, the variable storing the current room will continue with a valid room (the current one).

When returning a non-predictable value, it will most likely not be one of the correct room names, which it needs to in order to keep the logical construct on track. Once the room variable contains garbage, it can never match any of the options anymore and therefor does not output anything meaningful ever again.

As an additional precaution against the room ending up garbage (by one of any number of possible accidents) you can check that the room is one of the existing ones and otherwise reset it to a default - or exit with an error message
"Oops, unexpected teleport to unknown space. See you back on earth next time."

Yunnosch
  • 26,130
  • 9
  • 42
  • 54