Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

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

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.

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

Thank you in advance!!

>Solution :

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

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading