我试图允许用户忽略我在 Python 中基于文本的游戏在类中的大小写



我知道.lower((和.opper((,但不管怎样,它们似乎都不起作用,只会导致我的指示根本不起作用。任何帮助都将不胜感激,因为这是一个周日到期的项目,我真的很困。我的所有代码如下:

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时,请确保它在作为输入读取的字符串上,而不是在通过调用.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()

相关内容

  • 没有找到相关文章

最新更新