I have been learning python by just kinda messing around and looking up tutorials online, but I can't seem to figure out why this code isn't working for this "game" I'm making. I want the player to see what items are on a list, and I want them to be able to take things from that list by typing what they want to take. The problem I have been running into is that the game displays the wrong line of text when you take an item. Instead of saying "You take the a" like it is supposed to it prints the "Invalid command" line, but it still adds the 'a' to the player's inventory. I can not for the life of me figure out why it is still adding the item without printing the correct text.
inventory=[]
closet=['A','B','C']
Closetloop=False
while Closetloop==False:
print('Inside Your closet You have:')
sleep(1)
print(closet)
sleep(2)
print('What items do you take out? (Type "exit" to exit closet))
ClosetTake=input('You take:')
sleep(1)
if ClosetTake.lower()=='a':
if 'A' in closet:
os.system('cls')
print('You take the a')
res = inventory.insert(0, closet.pop(closet.index('A')))
Closetloop=False
else:
os.system('cls')
print('Invalid command')
Closetloop=False
if ClosetTake.lower()=='b':
if 'B' in closet:
os.system('cls')
print('You take the b')
res = inventory.insert(0, closet.pop(closet.index('B')))
Closetloop=False
else:
os.system('cls')
print('Invalid command')
Closetloop=False
if ClosetTake.lower()=='c':
if 'C' in closet:
os.system('cls')
print('You take the c')
res = inventory.insert(0, closet.pop(closet.index('C')))
Closetloop=False
else:
os.system('cls')
print('Invalid command')
Closetloop=False
if ClosetTake.lower()=='exit':
os.system('cls')
print('You exit the closet')
Closetloop=True
CodePudding user response:
I've rewritten the code to show some other Python features which may be of interest.
inventory=set()
closet ={'A','B','C'}
while True:
if closet:
print(closet)
closet_take = input('Which item to take else \"exit\" to leave ').upper()
if closet_take == 'EXIT':
print(inventory)
print('bye')
break
if closet_take in closet:
closet.remove(closet_take)
inventory.add(closet_take)
continue
else:
print('Invalid Entry')