Featured Posts

CS50 Python , Nutrition Facts Table

Image
Nutrition Facts: Python Practice for Beginners Nutrition Facts Table for Python Practice Welcome to this comprehensive guide for Python beginners! If you are learning how to work with lists, dictionaries, and loops, this post will help you build practical skills using a real-world example: nutrition facts for fruits. Understanding how to organize and manipulate data is a key part of programming, and this exercise will give you hands-on experience. Below is a sample table of fruits and their calorie values, formatted as a Python list of dictionaries. This structure is ideal for coding exercises, projects, or even building your own nutrition calculator. You can expand this list, add new fruits, or use it as a foundation for more advanced Python tasks. Python List of Dictionaries Example: fruits = [ {'name': 'Apple', 'calories': 130}, {'name': 'Avocado', 'calories': 50}, {'name': 'Banana', 'ca...

Python 2.4.1.6 LAB: A LED Display

Python Code to make a Seven Led Display

Python Code to make a Seven Led Display
Seven LED Display

Exactly, what you see in the picture on top is a sample of a seven led display which uses a decimal number from 0-9 for as many digits as needed. Also, alpha characters (letters and symbols)can be used.

Seven LED Display
Figure.1

Why is it called "Seven Segment"?

Simply because it contains seven elements as shown in Figure.1. The seven segments are arranged as a rectangle of two vertical segments on each side with one horizontal segment on the top, middle, and bottom. The optional decimal point (an "eighth segment", referred to as DP) is used for the display of non-integer numbers.

Seven LED Display
Figure.2

As shown in Figure.2, this is how the numbers are shown. For example, "0" means all segments from A-G are led(shown), "1" means B and C are shown.

And below table shows us the plan of our code

Seven Segments-LED display
Seven Segments-LED display

If you want to get a full idea about How Seven Segment LED Display works, you will find a slideshow in the end of the post.

Solution Code

We will code/draw each segment as "#". Note, the "horizontal segments" have ###, "vertical segments" have #. So, "0" will look like below:

###
#  #
#  #
#  #
###

While "8" will show like this in the console:

###
#  #
###
#  #
###

dict1 = {
    '0': ('###', '# #', '# #', '# #', '###'),
    '1': ('  #', '  #', '  #', '  #', '  #'),
    '2': ('###', '  #', '###', '#  ', '###'),
    '3': ('###', '  #', '###', '  #', '###'),
    '4': ('# #', '# #', '###', '  #', '  #'),
    '5': ('###', '#  ', '###', '  #', '###'),
    '6': ('###', '#  ', '###', '# #', '###'),
    '7': ('###', '  #', '  #', '  #', '  #'),
    '8': ('###', '# #', '###', '# #', '###'),
    '9': ('###', '# #', '###', '  #', '###'),
    '.': ('   ', '   ', '   ', '   ', '  #'),
}
number = input("Please Enter a Positive Number: ")  
def seven_segment(number):
    for i in range(5):
        digits = [dict1[digit] for digit in str(number)]
        print("  ".join(segment[i] for segment in digits))
seven_segment(number)

And here's the Seven-Segment LED system information for further readings:

A LED Display
Seven Segments System



Image Source: https://www.electronics-tutorials.ws/wp-content/uploads/2013/10/segment4.gif?fit=277%2C236?fit=277,226

Comments

Popular posts from this blog

فرصتك للدراسة في ألمانيا: منحة ممولة بالكامل لطلاب الدراسات العليا

How to Deactivate Screen Reader in Kali Linux

Murphy's Law: Expect the Unexpected

Python 3.2.1.14 LAB: Essentials of the while loop

Data Analysis Roadmap 2026: From Excel Lover to Python-Powered Analyst