1

I'm creating a work log where a user can input a task or lookup a task by dates.

When looking up task by date, the user is presented with a list of dates. The user can then select from the list by entering a number. The list of tasks should then display for that specific date.

I'm just having a problem if the user enters a number not in the list of dates. You can see else statement at the end of method which is commented out---which had been causing a few issues. Everything works fine otherwise. So question--how do i provide error message if input from user is not in index list without printing out the message every time it loops through the list?

def search_by_date(self):
    for i, d in enumerate(self.tasklist.dates()):
        enum_list = [(i+1,d) for i,d in enumerate(self.tasklist.dates())]
        print(i+1, ':', d)
    while True:
        datereq = input("Select Number To See Tasks For A Date: ").strip()
        try:
            datereq = int(datereq)

        except ValueError:
            print("Invalid Entry")
            continue

        else:
            for i, d in enum_list:
                for task in self.tasklist.task_list:
                    if datereq == i:
                        if task.date == d:
                            print("Date :", task.date,
                                  " Task:", task.task,
                                  " Minutes:", task.minutes,
                                  " Notes: ", task.notes
                                  )
                            continue

                    #else:
                        #print("Invalid Entry. Please try again")
                        #continue
1
  • continue should probably be break. The continue statement has no effect here, it doesn't skip anything. Commented Mar 25, 2017 at 20:18

1 Answer 1

2

Is this what you're looking for?

def search_by_date(self):
    for i, d in enumerate(self.tasklist.dates()):
        enum_list = [(i+1,d) for i,d in enumerate(self.tasklist.dates())]
        print(i+1, ':', d)
    while True:
        datereq = input("Select Number To See Tasks For A Date: ").strip()
        try:
            datereq = int(datereq)

        except ValueError:
            print("Invalid Entry")
            continue

        else:
            found = False
            for i, d in enum_list:
                for task in self.tasklist.task_list:
                    if datereq == i and task.date == d:
                        found = True
                        print("Date :", task.date,
                              " Task:", task.task,
                              " Minutes:", task.minutes,
                              " Notes: ", task.notes
                              )
            if not found:
                print("Invalid Entry. Please try again")
Sign up to request clarification or add additional context in comments.

9 Comments

that's strange: it will print "invalid entry" whatever the result is.
Yes, without his data objects I can't actually run the code :) One sec as I fix
Hi Julien--yes when i try this it prints all the right results, but prints the Invalid Entry line at the end. For an actual invalid entry it is working correctly. Hmm..
I thought of an else statement matching the for loop but there are 2 for loops. you'll have to go with a flag telling that it's found.
nitpick: don't use == True or != True. Just use if found: and if not found
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.