برمجة الحاسب ببايثون 1 كلية الحاسب 47 شرح المشروع
Loading video…

شرح المشروع

El Cato Instructor: El Cato

Description

ينتهي الشرح عند الدقيقة 55

لا تنسى تضيف تعليقات (comments) تشرح الكود، اتكلمت عنها بالدقيقة 60

نسخة اخرى من الكود:

# مشروع: محاكاة صرّاف آلي (ATM)
# المستوى: مبتدئين
# الفكرة: البرنامج يسمح لعدة مستخدمين بالدخول باستخدام اسم مستخدم و PIN
#          ثم يتيح لهم سحب، إيداع، فحص الرصيد، بالإضافة لتخزين سجل العمليات

# ------------------------------
# 1. إنشاء بيانات المستخدمين
# ------------------------------
# نستخدم قاموس (Dictionary) لتخزين بيانات كل مستخدم
# كل مستخدم يحتوي على:
#   - رقم سري PIN
#   - رصيد Balance
#   - قائمة العمليات Transactions
users = {
    "ahmed": {"pin": "1111", "balance": 1000, "transactions": []},
    "sara": {"pin": "2222", "balance": 1500, "transactions": []},
    "omar": {"pin": "3333", "balance": 2000, "transactions": []}
}


# ----------------------------------------
# 2. إنشاء قائمة ثابتة للخيارات (Tuple)
# ----------------------------------------
# نستخدم tuple لأنها غير قابلة للتغيير
menu_options = ("Withdraw", "Deposit", "Check Balance", "Exit")


# ----------------------------------------
# 3. تعريف دوال العمليات
# ----------------------------------------

def withdraw(current_user):
    # دالة السحب
    # تأخذ اسم المستخدم وتطرح المبلغ من رصيده لو كان كافي
    print("\n*** Withdraw Money ***")
    
    # نطلب من المستخدم إدخال مبلغ السحب
    amount = float(input("Enter amount to withdraw: "))
    
    # نتحقق هل الرصيد يكفي
    if amount <= users[current_user]["balance"]:
        # ننقص الرصيد
        users[current_user]["balance"] -= amount
        
        # نسجل العملية في قائمة العمليات
        users[current_user]["transactions"].append(f"Withdrew {amount}")
        
        print("Withdrawal successful!")
    else:
        print("Error: Not enough balance!")


def deposit(current_user):
    # دالة الإيداع
    print("\n*** Deposit Money ***")
    
    # نطلب من المستخدم إدخال مبلغ الإيداع
    amount = float(input("Enter amount to deposit: "))
    
    # نضيف المبلغ للرصيد
    users[current_user]["balance"] += amount
    
    # نسجل العملية في سجل العمليات
    users[current_user]["transactions"].append(f"Deposited {amount}")
    
    print("Deposit successful!")


def check_balance(current_user):
    # دالة عرض الرصيد
    print("\n*** Check Balance ***")
    
    # نطبع رصيد المستخدم الحالي
    print("Your balance is:", users[current_user]["balance"])
    
    # نعرض أيضاً سجل العمليات
    print("\nTransaction History:")
    if users[current_user]["transactions"]:
        # إذا كان هناك عمليات سابقة
        for t in users[current_user]["transactions"]:
            print("-", t)
    else:
        print("No transactions yet.")


# ----------------------------------------
# 4. النظام الأساسي للدخول (Login System)
# ----------------------------------------
print("====================================")
print("      Welcome to Python ATM")
print("====================================")

# نطلب من المستخدم إدخال اسم المستخدم
username = input("Enter your username: ")

# التحقق إن كان اسم المستخدم موجود في القاموس
if username in users:
    
    # نطلب منه إدخال الـ PIN
    pin = input("Enter your PIN: ")
    
    # نتحقق إذا الـ PIN صحيح
    if pin == users[username]["pin"]:
        print("\nLogin successful!")
        
        # نعرض قائمة الخيارات
        while True:
            print("\n===== ATM MENU =====")
            
            # نطبع كل خيار من الـ Tuple
            for i in range(len(menu_options)):
                print(i + 1, "-", menu_options[i])
            
            # نطلب من المستخدم اختيار عملية
            choice = int(input("Choose an option: "))
            
            # نستخدم if للتعامل مع كل خيار
            if choice == 1:
                withdraw(username)
            elif choice == 2:
                deposit(username)
            elif choice == 3:
                check_balance(username)
            elif choice == 4:
                print("Thank you for using ATM. Goodbye!")
                break
            else:
                print("Invalid choice. Try again.")
    else:
        print("Incorrect PIN. Access denied.")
else:
    print("Username not found.")

Comments

Log in to join the discussion. Log in