0

I am currently making a MUD, or a SUD as we can call it (Singleplayer) The only way to navigate through the game is to type commands.

def level_01_room_01():
    choice = raw_input('>: ')
    if choice == 'north':
        level_01_room_02()

In this case, if the user enters North, with a capital N, the code will not perceive this command. It will be a big mess if I actually have to enter:

def level_01_room_01():
    choice = raw_input('>: ')
    if choice == 'North':
        level_01_room_02()
    if choice == 'north':
        level_01_room_02()
    if choice == 'NORTH':
        level_01_room_02()

Etc.

Is there any way I can fix this, so that the player can type the word exactly as he or she wants to?

Blckknght
  • 100,903
  • 11
  • 120
  • 169
Stig
  • 77
  • 2
  • 7
  • 1
    The usual python method for handling multiple choice questions is a dictionary, rather than a lot of if== tests. http://stackoverflow.com/questions/594442/choosing-between-different-switch-case-replacements-in-python-dictionary-or-if – theodox Aug 08 '13 at 01:15

3 Answers3

5

Always lower() the user input before comparing it against your known strings (and always use lower case).

if choice.lower() =='north':
    ...
Carl Groner
  • 4,149
  • 1
  • 19
  • 20
4

You can use

if choice.lower() == 'north':
    level_01_room_02()

(see str.lower())

arshajii
  • 127,459
  • 24
  • 238
  • 287
  • 2
    Or, if there are other things to match against, consider using `choice = raw_input('>: ').lower()` so you don't need to call `lower` repeatedly. – Blckknght Aug 08 '13 at 01:09
3

You can use the methods .lower() and .upper() to convert strings to the respective cases. A practical example would be lowercasing everything the user types like so:

def level_01_room_01():
choice = raw_input('>: ').lower()
if choice == 'north':
    level_01_room_02()

Since the input is always lowercased, as long as all your choices are coded in lower case ("north" and not "NORTH" or "North") it will match correctly with the previously lowercased input.