Search code examples
pythonvariablespython-3.xrandomintrospection

Print a variable selected by a random number


I have a list of names, and I would like my program to randomly select one of those names. I tried using the following:

import random


def main():

    Arkansas = 1
    Manchuria = 2
    Bengal = "3"
    Baja_California = 4
    Tibet = 5
    Indonesia = 6
    Cascade_Range = 7
    Hudson_Bay = 8
    High_Plains = 9
    map = random.randrange(1, 10)
    print(map)

main()

I also tried making each of the numbers as strings, using the eval()function for randrange(), but none of this worked.


Solution

  • Don't assign numbers OR strings. Use a list.

    choices = ['Arkansas', 'Manchuria', 'Bengal', 'Baja California']   # etc.
    

    Then take a random.choice

    random_choice = random.choice(choices)