בדיקת זמינות
חלק מהיחידה לוגיקה וזרימת תוכנית במסלול ה-Python של Coddy. שיעור 70 מתוך 78.
אתגר
קלצור פונקציה בשם check_availability שמקבלת ארגומנט אחד: item (מחרוזת). הפונקציה צריכה:
- לבדוק אם הפריט קיים במילון
inventory.- אם הוא לא קיים, להחזיר
"Item not found".
- אם הוא לא קיים, להחזיר
- אם הפריט קיים, להחזיר את המלאי הנוכחי של הפריט.
הוסף (החלף) את קטע הקוד הבא בתחתית הקוד שלך:
add_item("Apple", 0.5, 100)
add_item("Banana", 0.2, 50)
update_stock("Apple", -20)
update_stock("Banana", 30)
print(check_availability("Apple")) # Should return 80
print(check_availability("Banana")) # Should return 80
print(check_availability("Orange")) # Should return "Item not found"נסו בעצמכם
inventory = {}
def add_item(item, price, stock):
if item in inventory:
print(f"Error: Item '{item}' already exists.")
return
try:
inventory[item] = {"price": float(price), "stock": int(stock)}
print(f"Item '{item}' added successfully.")
except ValueError:
print("Error: Price and stock must be numeric.")
def update_stock(item, quantity):
if item not in inventory:
print(f"Error: Item '{item}' not found.")
return
try:
new_stock = inventory[item]["stock"] + int(quantity)
if new_stock < 0:
print(f"Error: Insufficient stock for '{item}'.")
else:
inventory[item]["stock"] = new_stock
print(f"Stock for '{item}' updated successfully.")
except ValueError:
print("Error: Quantity must be an integer.")
add_item("Apple", 0.5, 100)
add_item("Banana", 0.2, 50)
add_item("Apple", 0.6, 30) # Should print an error
update_stock("Apple", -20)
update_stock("Banana", 30)
update_stock("Orange", 10) # Should print an error
update_stock("Apple", -90)
print(inventory) כל השיעורים ביחידה לוגיקה וזרימת תוכנית
4יישום ספר אנשי קשר
הצגת התפריטהוספת איש קשר7קבוצות חלק 2
פעולות מתמטיות חלק 1פעולות מתמטיות חלק 2סיכום – חיפוש אוצרתת־קבוצות וקבוצות־עלמעבר על קבוצותסיכום – מעקב אחר טורניר8מנהל רישומי תלמידים
סקירת הפרויקטהוספת תלמידתרגלו בעצמכם: קומפיילר Python אונליין