2

I am attempting an exercise where the user inputs a positive integer and the output should be a seven-segment display conversion.

Each digit is converted using hashed symbols so whatever digit is inputted, each of those individual numbers are converted to hashed symbols and added to empty string.

Here is my code:

zero = '###\n# #\n# #\n# #\n###\n\n'
one = '#\n#\n#\n#\n#\n\n'
two = '###\n  #\n###\n#  \n###\n\n'
three = '###\n  #\n###\n  #\n###\n\n'
four = '# #\n# #\n###\n  #\n  #\n\n'
five = '###\n#  \n###\n  #\n###\n\n'
six = '###\n#  \n###\n# #\n###\n\n'
seven = '###\n  #\n  #\n  #\n  #\n\n'
eight = '###\n# #\n###\n# #\n###\n\n'
nine = '###\n# #\n###\n  #\n###\n\n'

    def seven_segment_display(digit):
      if digit >= 0:
        digit = str(digit)
        digit_list = list(digit)
        new_digit = ''
        for i in range(len(digit_list)):
            if digit_list[i] == '0':
                new_digit += zero
            if digit_list[i] == '1':
                new_digit += one
            if digit_list[i] == '2':
                new_digit += two
            if digit_list[i] == '3':
                new_digit += three
            if digit_list[i] == '4':
                new_digit += four
            if digit_list[i] == '5':
                new_digit += five
            if digit_list[i] == '6':
                new_digit += six
            if digit_list[i] == '7':
                new_digit += seven
            if digit_list[i] == '8':
                new_digit += eight
            if digit_list[i] == '9':
                new_digit += nine
    return new_digit
print(seven_segment_display(123))

When I run the code, it outputs converted digits vertically as opposed to horizontally.

I am trying to output each of the converted digits on the same line.

I tried stripping newline but that still didn't print out each digit horizontally.

Any ideas on how to do this? I'm new to Python so any help would be appreciated!

Konami11
  • 31
  • 1
  • 1
  • 3

1 Answers1

13

First, use a dictionary to hold your representations. This is much more concise than having a whole bunch of variables and corresponding if statements.

Second, you will need to store each horizontal line of the 7-segment display separately, since you can only print left-to-right top-to-bottom. You can assign a 5-tuple for each number for this.

Based on your input of numbers, make a list of these representations. Once you have all of them, then join them together and print line-by-line.

representations = {
    '0': ('###', '# #', '# #', '# #', '###'),
    '1': ('  #', '  #', '  #', '  #', '  #'),
    '2': ('###', '  #', '###', '#  ', '###'),
    '3': ('###', '  #', '###', '  #', '###'),
    '4': ('# #', '# #', '###', '  #', '  #'),
    '5': ('###', '#  ', '###', '  #', '###'),
    '6': ('###', '#  ', '###', '# #', '###'),
    '7': ('###', '  #', '  #', '  #', '  #'),
    '8': ('###', '# #', '###', '# #', '###'),
    '9': ('###', '# #', '###', '  #', '###'),
    '.': ('   ', '   ', '   ', '   ', '  #'),
}

def seven_segment(number):
    # treat the number as a string, since that makes it easier to deal with
    # on a digit-by-digit basis
    digits = [representations[digit] for digit in str(number)]
    # now digits is a list of 5-tuples, each representing a digit in the given number
    # We'll print the first lines of each digit, the second lines of each digit, etc.
    for i in range(5):
        print("  ".join(segment[i] for segment in digits))

Proof of concept:

>>> seven_segment(98765432.01)
###  ###  ###  ###  ###  # #  ###  ###       ###    #
# #  # #    #  #    #    # #    #    #       # #    #
###  ###    #  ###  ###  ###  ###  ###       # #    #
  #  # #    #  # #    #    #    #  #         # #    #
###  ###    #  ###  ###    #  ###  ###    #  ###    #
Green Cloak Guy
  • 23,793
  • 4
  • 33
  • 53
  • 2
    Thanks so much! I looked at the solution for this problem and it was much more complicated to understand. Yours is a lot clearer. – Konami11 Nov 06 '19 at 02:38