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.5.1.9 LAB: The Digit of Life

 The Digit of Life

The digit of life

Life Path Number Calculator

To know your life path number, You need to enter your birth date as numbers only to calculate your life path number. You should add the digits of the month, day, and year until you arrive at a single digit number.

How digit of life  is calculated
How digit of life  is calculated

Let's see the lab question.

Objectives

  • improving the student's skills in operating with strings;
  • converting integers into strings, and vice versa.

Scenario

Some say that the Digit of Life is a digit evaluated using somebody's birthday. It's simple - you just need to sum all the digits of the date. If the result contains more than one digit, you have to repeat the addition until you get exactly one digit. For example:
  • 1 January 2017 = 2017 01 01
  • 2 + 0 + 1 + 7 + 0 + 1 + 0 + 1 = 12
  • 1 + 2 = 3
3 is the digit we searched for and found.

Your task is to write a program which:
  • asks the user her/his birthday (in the format YYYYMMDD, or YYYYDDMM, or MMDDYYYY - actually, the order of the digits doesn't matter)
  • outputs the Digit of Life for the date.
Test your code using the data we've provided.

Test data

Sample input:
19991229
Sample output:
6
Sample input:
20000101
Sample output:
4

Solution Code

Solution #1

text = (input("Please enter your birthday as numbers only: "))
if len(text) != 8 or not text.isdigit():
    print("Date format is invalid")
else:
    while len(text) > 1:
        the_sum = 0
        for i in text:
            the_sum += int(i)
        text = str(the_sum)
    print(text)

Solution #2

# date of birth
dob = input("Please enter your birthday as numbers only: ")
# digit of life
dol = 0
for num in dob:
    dol += int(num)
       
    if dol > 9:
        dol = dol % 10 + dol // 10
# printing result
print("The Digit of Life Number: " + str(dol))

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