簡體   English   中英

我試圖讓用戶忽略我在 Python 中為 class 的文本游戲的情況

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

我知道 about.lower() 和 .upper() 但無論它們似乎不起作用,它們只會導致我的指示根本不起作用。 任何幫助將不勝感激,因為這是針對周日到期的項目,我真的被困住了。 我的所有代碼如下:

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.')

主要的()

我不知道這是否是您的問題,但是當您調用lower 時,請確保它在作為輸入讀取的字符串上,而不是在calling.split() 創建的數組上。

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

可能是你正在尋找的。

directions列表中的方向是標題大小寫的(例如"North" ),因此.upper (會產生例如"NORTH" )和.lower (會產生例如"north" )都不會使它們相等。

command[1].title()將以與列表中的方向相同的方式格式化輸入。 或者,您可以將方向存儲為全小寫或全大寫並使用command[1].lower()command[1].upper()

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM