How Do I Change My Timezone?

Sorry for confusing title. This program should take the letter in a string and add the word ‘List’ so that it can find the 2 list groups I created. Python is taking every character in the word ‘hList’ instead of every value in the variable hList. Is there any other way to do this?

The output I’m expecting is: 2 5 3 6 6 2

userInput = 'hi'
hList = [2, 5, 3]
iList = [6, 6, 2]
userInputLen = len(userInput)
for i in range (0, userInputLen):
    for objects in (userInput[i] + 'List'):
        print(objects)

2 ANSWERS

February 16, 2017 at 10:13 am admin

see also: https://bdhacker.wordpress.com/2010/02/27/python-tutorial-dictionaries-key-value-pair-maps-basics/

userInput = 'hi'
lists = {}
lists['h'] = [2, 5, 3]
lists['i'] = [6, 6, 2]
for i in userInput:
    for objects in (lists[userInput[i]]):
        print(objects)
February 16, 2017 at 10:13 am admin

Use eval :

userInput = 'hi'
hList = [2, 5, 3]
iList = [6, 6, 2]
userInputLen = len(userInput)
for i in range (0, userInputLen):
    for objects in eval(userInput[i] + 'List'):
        print(objects)

Without eval,userInput[i] + ‘List’ is a string;with eval,it points to a variable called hList or iList.

Viewing 2 replies - 1 through 2 (of 2 total)

You must be logged in to reply to this topic.