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