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

How can I get python to output just string out of a list?

import random as r

list = ['left','right','center']

shoot = r.choices(list)
print('python says, shoot ' + str(shoot))

How can I get it to output just left or right or center instead of ['left']

>Solution :

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

The method random.choices make multiples choice, use random.choice

import random as r

values = ['left','right','center']

shoot = r.choice(values)
print('python says, shoot', shoot)

Note

  • don’t use list as variable name, that’s python list constructor
  • shoot is already a string, no need of 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