0

New to learning Python and trying to test a few things out to better understand a better way to code within Python. For this question what I'm trying to do is, have the user input an answer from a list of options to then move onto the next function. So e.g.,

dnd_weapons = ["club", "great axe", "longbow", "hand crossbow"]

user_weapon = input("What weapon would you like to select? ", dnd_weapons)

The input I would like the user to see is:

What weapon would you like to select? club, great axe, longbow, hand crossbow

Currently I'm running into errors of getting this to work like the above code does not work. Hopefully this makes sense, just trying to find a better way to do an input from a list instead of re-writing the dnd_weapons over and over again in different sections of code.

Thank you in advance!!

2
  • input makes a string. So use split to separate it into parts based on a delimiter. In this case your delimiter is , (comma and space between each weapon) Commented Nov 15, 2021 at 16:25
  • "Currently I'm running into errors of getting this to work" What are the exact errors? How do you want to output the array? What do you want it to look like on the screen? Commented Nov 15, 2021 at 16:28

2 Answers 2

1

Here's a simple way to do that using the join() method and some formatted strings:

dnd_weapons = ["club", "great axe", "longbow", "hand crossbow"]
dnd_weapons_str=', '.join(dnd_weapons)
user_weapon=input(f"What weapon would you like to select?{dnd_weapons_str}: ")
Sign up to request clarification or add additional context in comments.

1 Comment

Yes! This is exactly what I was looking for! I was unaware of the join function in Python, very good to know! Thank you for this, really appreciate it!!
1

This is not an easy thing to accomplish in vanilla python, but there's a python library for this that's worked well for me in the past.

https://github.com/wong2/pick

 pip install pick

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.