简体   繁体   中英

I'm trying to allow the user to ignore the case for my text-based game in Python for a class

I know about.lower() and.upper() but no matter what they don't seem to work and they just cause my directions to not work at all. Any help would be appreciated as this is for a project due Sunday and I'm really stuck. All of my code is as follows:

def menu():
print('*' * 20)
print("InSIDious: Sid & the Commodore 64")
print('*' * 20)
print("Collect the 6 pieces of the Commodore 64 before facing Death Adder.")
print("Otherwise succumb to him and be stuck in this realm forever!")
print("How to play: To move, enter 'go North', 'go East', 'go South', 'go West' or 'Exit'/'exit' to quit playing.")
print("To pick up items, enter 'get Item Name' and it will be added to your inventory.")
print("Good luck!\n")

def Main():        
    rooms = {
        'Court Yard': {'North': 'Great Hall', 'East': 'Bed Chambers', 'South': 'Gate House', 'West': 'Kitchen'},
        'Great Hall': {'East': 'Throne Room', 'South': 'Court Yard', 'item': 'Sockets'},
        'Bed Chambers': {'North': 'Bathroom', 'West': 'Court Yard', 'item': 'Semiconductors'},
        'Gate House': {'North': 'Court Yard', 'East': 'Chapel', 'item': 'Capacitors'},
        'Kitchen': {'East': 'Court Yard', 'item': 'Connectors'},
        'Throne Room': {'West': 'Great Hall', 'item': 'Resistors'},
        'Bathroom': {'South': 'Bed Chambers', 'item': 'Filters'},
        'Chapel': ''
    }
def user_status():
    print('-' * 20)
    print('You are currently in the {}'.format(current_room))
    print('Inventory:', inventory)
    print('-' * 20)

directions = ['North', 'South', 'East', 'West']
current_room = 'Court Yard'
inventory = []
menu()

while True:
    if current_room == 'Chapel':
        if len(inventory) == 6:
            print('-----------------')
            print('Congratulations!')
            print('-----------------')
            print('You can now return home after collecting the 6 pieces of the Commodore 64')
            print('& defeating Death Adder!')
            print('Thank you for playing!')
            break
            # Losing condition
        else:
            print('Oh no! You have been found by Death Adder before acquiring all the items to defeat him!')
            print('You are now trapped in this realm forever!')
            print('Thank you for playing!')
            break
    print()
    user_status()
    dict1 = rooms[current_room]
    if 'item' in dict1:
        item = dict1['item']
        if item not in inventory:
            print('You see the {} in this room'.format(rooms[current_room]['item']))

    command = input('What would you like to do?\n').split()
    if command[0] == 'go':
        if command[1] in directions:
            dict1 = rooms[current_room]
            if command[1] in dict1:
                current_room = dict1[command[1]]
            else:
                print('You cannot go that way.')

    elif command[0] in ['exit', 'Exit']:
        print('Thank you for playing, play again soon!')
        break
    elif command[0] == 'get':
        if command[1] == item:
            inventory.append(item)
            print('You picked up the' + item)
        else:
            print('Invalid command.')
    else:
        print('Invalid input, try again.')

Main()

I have no idea if this is your problem or not, but when you call lower, make sure it's on the string read as input, and not on the array created by calling.split().

command = input('What would you like to do?\n').lower().split()

might be what you're looking for.

The directions in the directions list are in title case (eg "North" ), so neither .upper (which would yield eg "NORTH" ) nor .lower (which would yield eg "north" make them equal.

command[1].title() will format the input in the same way as the directions in the list. Alternatively, you could store the directions in all-lowercase or all-uppercase and use command[1].lower() or command[1].upper() .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM