简体   繁体   English

我试图让用户忽略我在 Python 中为 class 的文本游戏的情况

[英]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.我知道 about.lower() 和 .upper() 但无论它们似乎不起作用,它们只会导致我的指示根本不起作用。 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().我不知道这是否是您的问题,但是当您调用lower 时,请确保它在作为输入读取的字符串上,而不是在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. directions列表中的方向是标题大小写的(例如"North" ),因此.upper (会产生例如"NORTH" )和.lower (会产生例如"north" )都不会使它们相等。

command[1].title() will format the input in the same way as the directions in the list. command[1].title()将以与列表中的方向相同的方式格式化输入。 Alternatively, you could store the directions in all-lowercase or all-uppercase and use command[1].lower() or command[1].upper() .或者,您可以将方向存储为全小写或全大写并使用command[1].lower()command[1].upper()

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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