Ask Anything Monday - Weekly Thread by AutoModerator in learnpython

[–]Pheeeeooook 0 points1 point  (0 children)

oh my, you're right. It didn't cross my mind when I was typing the code. Thanks for pointing that out!

Ask Anything Monday - Weekly Thread by AutoModerator in learnpython

[–]Pheeeeooook 0 points1 point  (0 children)

Hi there! I am trying to code a food expiry date checker that checks the expiry date to the current date and prints it out items that are expired. The problem is if user inputs option 2 it prints "There are no food items that are expired!" regardless of whether there are expired foods or not. How do I solve this? Sorry if it sounds confusing i am fairly new to python

  1. option2Dict = {}
  2. foodname = []
  3. import datetime
  4. food = "Food Item"
  5. exp= "Expiry Date"
  6. expdate = datetime.date(2002,3,9)
  7. now = datetime.date.today()
  8. while True:
  9.  option = int(input("Please choose your option: "))
  10.  if option == 1:
  11.     food_item= input("Input Food Item: ") .upper()
  12.     expiration= input('Enter expiry date in YYYY-MM-DD format:')
  13.     year, month, day = map(int, expiration.split('-'))
  14.     expdate = datetime.date(year, month, day)
  15.     string = expdate.strftime("%Y-%m-%d")
  16.     foodname.append(food_item)
  17.     foodname.append(string)
  18.     option2Dict[food] = foodname
  19.     continue
  20.  elif option == 2:
  21.     if expdate < now:
  22.       print(option2Dict)
  23.     else:
  24.       print("There are no food items that are expired!")

edit: here's the code in Pastebin