Home > Software engineering >  How to change list depend in string value?
How to change list depend in string value?

Time:05-08

I want to print list depend on input string

The code like this:

A1 = [1,2]
B1 = [2,3]

p = input("A1/B1 : ")

print(p)
#if input is "A1" it will print A1 list or "A2" it will print A2

CodePudding user response:

Yes, you can store your data in a dict and get it to retrieve the data you want:

d = {'A1': [1,2], 'B1': [2,3]}

p = input("A1/B1 : ")

element = d.get(p, None)
if element is None:
    print("Sorry, not found")
else:
    print(element)

CodePudding user response:

You're looking for something like?

if p == 'A1':
  print(A1)
elif p == 'A2':
  print(A2)
else:
  print('Your answer must be A1 or A1')
  • Related