Ask coding questions

← Back to all posts
How to do this?
sutartpoool (8)

I am making some python text adventures, and I need to know how to make a response, but only for certain things that you say. Such as: What do you want to pick up?: list of items. Then you say one and it gets added to your inventory.

Answered by LukeWright (287) [earned 5 cycles]
View Answer
LukeWright (287)

how about something like this:

choices = ["diamonds","iron","dirt"]
print(f"Your choices are {choices}")
while True:
  choice = input("Choose one: ")
  if choice in choices:
    print(f"You picked up {choice}!")
    print("You did not choose one of the options! Try again!")

The above code will loop indefinately until you pick a choice from the list 'choices'. if you type anything but the exact spelling of one of the choices, it will notify you and then repeat over again so that they have another chance to pick something. This will prevent the code breaking if someone typed "diamond" instead of "diamonds"

VulcanWM (2766)


something = input(“what do u want to pickup?”)
if something == “list of items”:
  item = input(“enter item name”)
  #do whatever with item