N
N
neulwert2019-10-18 14:06:11
Python
neulwert, 2019-10-18 14:06:11

Bigdigits.py from Macle Summerfield's book, How does the program work?

I need help, I'm a beginner, I'm studying Macle Summerfield's book - "Programming in Python3", he has an excellent presentation of the material, but now the first difficulties have appeared, moreover, with the very first programs. I need the most important thing to understand the logic of the code. In addition, there is an error in the program and it would be great to fix it, but this is secondary, I will be glad if you just help me understand it.
Here is a screenshot of the code in pycharm, I checked it, everything is like in a book, I didn’t add anything of my own:
5da9979b89610760311971.png
I tried to delve into the program, read the explanation, the point is that we enter numbers (0123456789), and they are displayed drawn with asterisks. But, despite the explanations, there were misunderstandings in the code. Here it is with comments:

Digits = [Zero, One, Two, Three, Four, Five, Six, Seven, Eight, Nine]  # значения этих переменных добавлять не стал

try:
    digits = sys.argv[1]  # эта переменная содержит цифру, которую мы вводим, но зачем на конце элемент [1]?
                         # и как так вышло, что digits присваивается то, что я пишу в терминале?
    row = 0
    while row < 7:  # row специально содержит 0, чтобы условие было всегда True?
        line = ''  # здесь пустая строка, если вы не заметили))
        column = 0
        while column < len(digits):  # опять условие всегда True, т.к. длина цифры = 1, а column = 0
            number = int(digits[column])  # зачем вписывать [column]? Он же = 0? взять первый элемент из введенного числа?
            digit = Digits[number]  # к примеру если ввести 012, из списка Digits возьмутся первые три значения?
            line += digit[row] + '  '  # эти нагромождения списков меня путают... опять на конце ввели кортеж, который = 0
            column += 1  # к этому моменту я окончательно потерял нить логики в этой программе
        print(line)  # это должно выводить на экран полученный результат
        row += 1  # а это читать следующий элемент? Нет?
except IndexError:  # с этим все ясно.
    print('usage: bigdigits.py <number>')  # python bigdigits.py <number> так вызывается прога через терминал
except ValueError as err:
    print(err, 'in', digits)  # здесь python ругается на элемент digits "name digits can be not defined" (видно в скринах)

And here is the output:
5da996a80576e178699699.png
The error in the program is that the more numbers you write, the more they bend at the output, especially the latest ones. And even after the seven, the numbers are placed at an angle (this can be seen in the screenshot), I think this is because the distance between the numbers is incorrectly spelled out. I hope I have explained everything clearly.

Answer the question

In order to leave comments, you need to log in

2 answer(s)
O
o5a, 2019-10-18
@neulwert

column iterates over the digits entered by the user and for each one takes the corresponding figure from the Digits array.
row iterates the graphics of the figure from top to bottom, and prints line by line, while in each row a piece of the figure of the numbers of this row is "glued".
In order for the figures not to "move", the length of each element of the list of numbers (Zero, One, .., etc.) must be the same (6 in this example), in the sum of asterisks and spaces. It can be seen that, for example, the number 7 (the list of Seven) does not comply with this (the last element is generally '*' although it should be * and 5 spaces). Because of such inconsistencies, figures move out.

A
Alexander, 2019-10-18
@NeiroNx

It's more of an environment problem. Any character graphics are designed for the fact that the width and height of each character are the same, but in fact only MonoSpace fonts have such characteristics. Change the font and that's it.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question