Home > Blockchain >  Is there a way to output an array within an input in Python?
Is there a way to output an array within an input in Python?

Time:11-16

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!!

CodePudding user response:

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}: ")

CodePudding user response:

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
  • Related