-2

I want to code a calendar with two lists in the first are the dates and in the second are the events. I want that you have to input a date if its in the calendar list I want my code to search where it is in the list. Then my code should search what is in the events list at the same location and print the Event. Thanks for every anser.

p.s. I code with python since a few weeks so I am still a noob

calendar = ['01.02.2019', '02.02.2019']
termine = ['15:20 playing football', '17:30 playing basketball']


date = str(input('Date: '))
if (date in calendar):           
    print ('found');                                                   
        esindices = [i for i, x in enumerate(calendar) if x == date] 
        print (esindices)
        print(events[int(esindices)])

Date: 01.02.2019 found [0]

---------------------------------------------------------------------------
NameError                                 Traceback (most recent call last)
<ipython-input-17-00e8535c4c6c> in <module>
      8     esindices = [i for i, x in enumerate(calendar) if x == date]
      9     print (esindices)
---> 10     print(events[int(esindices)])

NameError: name 'events' is not defined

This is the Error which comes and I dont know how to solve this.

Patrick Artner
  • 50,409
  • 9
  • 43
  • 69
Felix
  • 3
  • 1

1 Answers1

0

You get a NameError - meaning python does not know something. This is often a scoping problem or sometimes you forgot to create a variable (or have a typo in your variablename).

For debugging you can comment out the line that errors out and print() instead ... to see what might be wrong - you can read some more tips on debugging: Can you step through python code to help debug issues?


Fault:

Your code has termine - not events and uses a wild mix of german, english and spanish(?).

Fix:

calendar = ['01.02.2019', '02.02.2019']
termine = ['15:20 playing football', '17:30 playing basketball']


date = str(input('Date: '))
if (date in calendar):           
    print ('found')                                         
    esindices = [i for i, x in enumerate(calendar) if x == date] 
    print (esindices)
    for index in esindices:  # this is a list, not a single index - you need to iterate over
        print(termine[index]) # fix naming

It would be better to use a dictionary - you can use real dates as key and a list of things to do as value:

import datetime

# your calender dictionary
cal = {}

# today
dt = datetime.date.today()

# put in some values
for i in range(5):
    cal[dt-datetime.timedelta(days=i)] = [f"look {i} vids"]    

# print default values
print(cal)

Output:

# defaults 
{datetime.date(2019, 1, 19): ['look 0 vids'], 
 datetime.date(2019, 1, 18): ['look 1 vids'], 
 datetime.date(2019, 1, 17): ['look 2 vids'], 
 datetime.date(2019, 1, 16): ['look 3 vids'], 
 datetime.date(2019, 1, 15): ['look 4 vids']}

Inputting more data:

# get input, split input
datestr,action = input("Date:action").split(":")  # 2018-01-25:Read a book

# make datestr into a real date
date = datetime.datetime.strptime(datestr.strip(),"%Y-%m-%d").date()

# create key if needed, append action to list (use a defaultdict - it is faster then this)
# if you feel you hit a speed-problem and want it to be more "optiomal"
cal.setdefault(date,[]).append(action)


# input another date:action
datestr,action = input("Date:action").split(":")  # 2018-01-25:Go out and party

# parse the date again
date = datetime.datetime.strptime(datestr.strip(),"%Y-%m-%d").date()

# this time the key exists and we add to that one
cal.setdefault(date,[]).append(action)

# print all
print(cal)

Output:

# after inputs:
{datetime.date(2019, 1, 19): ['look 0 vids'], 
 datetime.date(2019, 1, 18): ['look 1 vids'], 
 datetime.date(2019, 1, 17): ['look 2 vids'], 
 datetime.date(2019, 1, 16): ['look 3 vids'], 
 datetime.date(2019, 1, 15): ['look 4 vids'], 
 datetime.date(2018, 1, 25): ['Read a book', 'Go out and party']}

Doku: dict.setdefault

Patrick Artner
  • 50,409
  • 9
  • 43
  • 69
  • Thank you very much for your answer :).I have tryied your fix and it works now. But now I read your solution too and I hope I understand it. – Felix Jan 19 '19 at 12:43