مشروع حاولت اعمله اثناء و قت فراغي للتسلية وضعت فيه كل ما اعرفه تقريبا في الوقت اللي كتبت فيه طبعا

#!/usr/bin/python

user = input(“Hello User please enter name:\n”).strip().lower()

تحقق أن اسم المستخدم ليس فارغًا

if not user:
print(“You must enter a valid name!”)
else:
print(f"Welcome to {user}, the program hours time")

password_input = input("Please enter the password used to access the program:\n").strip()

# تحقق أن كلمة المرور تحتوي على 4 أرقام فقط
if len(password_input) != 4 or not password_input.isdigit():
    print("Password must be exactly 4 digits.")
else:
    password = int(password_input)

    email = input(f"Hello {user}, please enter your email:\n").strip().lower()

    # تحقق أن البريد الإلكتروني يتبع النمط المطلوب وغير فارغ
    if not email:
        print("You must enter a valid email!")
    elif password == 5555 and email == f"{user}@gmail.com":
        print("Welcome to the hour program")
        import time

        t = time.localtime(time.time())
        localtime = time.asctime(t)
        print("Current Time: " + localtime)

        try:
            int_age = int(input("How old are you?:\n").strip())
            
            # تحقق من أن العمر بين 5 و 100 سنة
            if int_age < 5 or int_age > 100:
                print("This is an incorrect age.")
            else:
                current_year = time.localtime().tm_year
                birth_year = current_year - int_age
                print(f"You were born in the year {birth_year}")
        except ValueError:
            print("Invalid age. Please enter a valid number.")
    else:
        print(f"Please try again next time, {user}")

ما رايكم جربوه عندكم

إعجابَين (2)

جميل فقط نصيحة على المنشور ، عند نسخك للكود ستجد أيقونة

فيها هذا الرمز ‹/› و تعني نص منسق مسبقا تعطيك هذا الشكل :

نص منسَّق سابقًا

الصق الكود في مكان جملة نص منسق مسبقا ، بهذا الشكل يطلع الكود مكتوب بشكل منسق على المنشور.

أنا اكتشفتها البارح فقط :grin::grin:

إعجابَين (2)

ماشاء الله موفق ان شاء الله

مشروعك يبدو رائعًا ويمثل تحديًا ممتازًا لاستخدام بعض مهارات البرمجة التي تعلمتها! إليك بعض النقاط التي قد تساعدك في تحسين وتجربة الكود الخاص بك:

نقاط القوة:

  1. التحقق من صحة البيانات:

    • قمت بالتحقق من صحة إدخالات المستخدم بشكل جيد، مثل التأكد من أن الاسم ليس فارغًا وأن كلمة المرور تحتوي على 4 أرقام فقط.
    • التحقق من صحة البريد الإلكتروني والعمر هو أيضًا خطوة ممتازة.
  2. التعامل مع الأخطاء:

    • استخدام try و except للتعامل مع الأخطاء المحتملة مثل إدخال عمر غير صحيح هو نهج جيد لتحسين استقرار البرنامج.
  3. استخدام مكتبة الوقت:

    • عرض الوقت الحالي وإجراء الحسابات بناءً على العمر يعكس فهمك لاستخدام مكتبة time في بايثون.

تحسينات محتملة:

  1. تحسين التحقق من البريد الإلكتروني:

    • يمكن تحسين التحقق من صحة البريد الإلكتروني بشكل أكبر باستخدام تعبيرات نمطية (Regex) للتأكد من أنه يتبع الصيغة الصحيحة للبريد الإلكتروني.
  2. تنظيم الكود:

    • يمكنك تحسين تنظيم الكود بتقسيمه إلى دوال. هذا سيساعد في جعل الكود أكثر وضوحًا وسهولة في الصيانة. على سبيل المثال، يمكنك كتابة دالة للتحقق من صحة البيانات وإدخال المستخدم.
  3. تجربة بعض الحماية للأمان:

    • على الرغم من أن البرنامج هو مجرد مثال تعليمي، إلا أنه من المفيد التفكير في تحسين الأمان، مثل استخدام كلمات مرور أكثر أمانًا أو تشفير المعلومات الحساسة.
  4. تحسين الرسائل النصية:

    • يمكن تحسين الرسائل النصية لجعلها أكثر وضوحًا وتفاعلًا مع المستخدم. على سبيل المثال، يمكنك إضافة تعليمات أكثر تفصيلًا حول كيفية إدخال المعلومات.

نسخة محسّنة من الكود:

#!/usr/bin/python

import time

def get_user_name():
    user = input("Hello User please enter your name:\n").strip().lower()
    if not user:
        print("You must enter a valid name!")
        return None
    return user

def get_password():
    password_input = input("Please enter the password used to access the program:\n").strip()
    if len(password_input) != 4 or not password_input.isdigit():
        print("Password must be exactly 4 digits.")
        return None
    return int(password_input)

def get_email(user):
    email = input(f"Hello {user}, please enter your email:\n").strip().lower()
    if not email:
        print("You must enter a valid email!")
        return None
    return email

def get_age():
    try:
        int_age = int(input("How old are you?:\n").strip())
        if int_age < 5 or int_age > 100:
            print("This is an incorrect age.")
            return None
        return int_age
    except ValueError:
        print("Invalid age. Please enter a valid number.")
        return None

def main():
    user = get_user_name()
    if user is None:
        return

    password = get_password()
    if password is None:
        return

    email = get_email(user)
    if email is None:
        return

    if password == 5555 and email == f"{user}@gmail.com":
        print("Welcome to the hour program")
        t = time.localtime(time.time())
        localtime = time.asctime(t)
        print("Current Time: " + localtime)

        age = get_age()
        if age is None:
            return

        current_year = time.localtime().tm_year
        birth_year = current_year - age
        print(f"You were born in the year {birth_year}")
    else:
        print(f"Please try again next time, {user}")

if __name__ == "__main__":
    main()

ملخص:

مشروعك يمثل خطوة ممتازة لتطبيق ما تعلمته. مع تحسينات صغيرة، يمكن أن يصبح أكثر كفاءة وسهولة في الاستخدام. استمر في التجربة والتعلم! إذا كانت لديك أي استفسارات أخرى أو تحتاج إلى المزيد من المساعدة، أنا هنا لدعمك.