user2212774
user2212774

Reputation: 215

Using raw_input to define what variable list to search

How can i use the raw_input value stored in rowChoice be used in count? i.e. right now i define A.count. How would i wtite the correct equivanlent of rowChoice.count, where the user defines which of A,B,C,D to seach through.

A = ['available', 'unavailable','available','available','available']
B = ['unavailable', 'unavailable','available','available','available']
C = ['available', 'unavailable','unavailable','available','available']
D = ['available', 'unavailable','available','unavailable','unavailable']

rowChoice = raw_input('What row would you like to sit in? >> ')

count = A.count('available')
print'there are %r seats available in this row' %(count)

Upvotes: 1

Views: 181

Answers (2)

Alok Nayak
Alok Nayak

Reputation: 2541

If you dont wanna use dictionary, You can use this function "ord"

ord('A')  #65 
ord('a')  #97  , then use
i = ord('A') - 65 # use i as index to your created list = [A,B,C,D]
created_list = [A,B,C,D]  #then
i = ord(rowchoice) - 65
count = created_list[i].count('available')

Upvotes: 0

jamylak
jamylak

Reputation: 133674

It would be best to use a dictionary for this:

d = {'A': ['available', 'unavailable','available','available','available'],
     'B': ['unavailable', 'unavailable','available','available','available'],
     'C': ['available', 'unavailable','unavailable','available','available'],
     'D': ['available', 'unavailable','available','unavailable','unavailable']}

letter = raw_input().upper()    
d[letter].count('available')

Upvotes: 3

Related Questions