[Python] Use a variable whose name is 'Value in Variable List'

Asked 2 years ago, Updated 2 years ago, 18 views

I'm a beginner who started Python last night. I know the basics because I did a little bit of C before, but it's a little harder than I thought. First of all, I'm sorry that the title of the question is ambiguous. There was no other way to express it. I was working on a code that took a three-digit number and represented it as a picture. It's my first time making it for practice, so it's a bit messyI'll just upload it.

zero = ["***",
        "* *",
        "* *",
        "* *",
        "***"]

one = [" * ",
       "** ",
       " * ",
       " * ",
       "***"]

two = ["***",
       "  *",
       "***",
       "*  ",
       "***"]

three = ["***",
         "  *",
         "***",
         "  *",
         "***"]

four = ["  *",
        " **",
        "* *",
        "***",
        "  *"]

five = ["***",
        "*  ",
        "***",
        "  *",
        "***"]

six = ["***",
       "*  ",
       "***",
       "* *",
       "***"]

seven = ["***",
         "  *",
         " * ",
         " * ",
         " * "]

eight = ["***",
         "* *",
         "***",
         "* *",
         "***"]

nine = ["***",
        "* *",
        "***",
        "  *",
        "***"]

listup = ["zero", "one", "two", "three", "four", "five", "six", "seven", "eight", "nine"]
N = 0


def Display(n):
    i = 0
    ycdn = 0
    bufferch = ["","",""]
    ln = ["", "", ""]
    while i <= 2:
        ln[i] = listup[int(n[i])]
        i += 1


N = input('Type 3-digit number: ')
str(N)
print(N[0], N[1], N[2])
print('\n\n')
Display(N)

Blocked in Display() function. The method I came up with was,

I wonder how we can replace the values of ln with variables in number 3. Or is there a better way? I'm sorry for the poor explanation.

python

2022-09-21 11:48

3 Answers

zero= ["***",
        "* *",
        "* *",
        "* *",
        "***"]

one = [" * ",
       "** ",
       " * ",
       " * ",
       "***"]

two = ["***",
       "  *",
       "***",
       "*  ",
       "***"]

three = ["***",
         "  *",
         "***",
         "  *",
         "***"]

four = ["  *",
        " **",
        "* *",
        "***",
        "  *"]

five = ["***",
        "*  ",
        "***",
        "  *",
        "***"]

six = ["***",
       "*  ",
       "***",
       "* *",
       "***"]

seven = ["***",
         "  *",
         " * ",
         " * ",
         " * "]

eight = ["***",
         "* *",
         "***",
         "* *",
         "***"]

nine = ["***",
        "* *",
        "***",
        "  *",
        "***"]

listup = ["zero", "one", "two", "three", "four", "five", "six", "seven", "eight", "nine"]
N = 0

def Display(n):
    buf = ''
    buf2 = ['']*5
    for i in n:
        tmp = 'listup[%d]'%int(i)
        buf = eval(tmp)
        for idx, j in enumerate(eval(buf)):
            buf2[idx] += j
            buf2[idx] += ' '
    for i in range(0,5):
        print(buf2[i])


N = input('Type 3-digit number: ')
str(N)
print(N[0], N[1], N[2])
print('\n')
Display(N)

#result
#Type 3-digit number: 456
#4 5 6

#  * *** *** 
# ** *   *   
#* * *** *** 
#***   * * * 
#  * *** *** 


2022-09-21 11:48

You do not need to make listup an array of "variable names".


zero = ["***",
        "* *",
        "* *",
        "* *",
        "***"]

one = [" * ",
       "** ",
       " * ",
       " * ",
       "***"]

two = ["***",
       "  *",
       "***",
       "*  ",
       "***"]

three = ["***",
         "  *",
         "***",
         "  *",
         "***"]

four = ["  *",
        " **",
        "* *",
        "***",
        "  *"]

five = ["***",
        "*  ",
        "***",
        "  *",
        "***"]

six = ["***",
       "*  ",
       "***",
       "* *",
       "***"]

seven = ["***",
         "  *",
         " * ",
         " * ",
         " * "]

eight = ["***",
         "* *",
         "***",
         "* *",
         "***"]

nine = ["***",
        "* *",
        "***",
        "  *",
        "***"]

listup = [zero, one, two, three, four, five, six, seven, eight, nine]
N = 0


def Display(n):
    screen = ['', '', '', '', '']
    for d in n:
        d = int(d)
        for i, l in enumerate(listup[d]):
            screen[i] += l
            screen[i] += '  '
    for l in screen:
        print(l)



N = input('Type 3-digit number: ')
N = str(N)
print(N)
print('\n\n')
Display(N)


2022-09-21 11:48

In the end, it's the same thing when you solve it,

Let's put a spoon on the code above.

def Display(n):
    for line in zip(*(listup[int(d)] for d in n)):
        print(" ".join(line))


2022-09-21 11:48

If you have any answers or tips

Popular Tags
python x 4647
android x 1593
java x 1494
javascript x 1427
c x 927
c++ x 878
ruby-on-rails x 696
php x 692
python3 x 685
html x 656

© 2025 OneMinuteCode. All rights reserved.