0

I am studying Python and now I'm doing a small project on my own. The for loop in the second function is not run through the whole code. When I run the code I get just the last element of the dictionary.

In order to solve it I have tried to use while but I got the same answer.

def enter_task():
    global num_task
    num_task = int(input("Please, enter number of tasks: "))
    calendar = {}
    for i in range(0, num_task):
        global task
        task=input("Please, enter task: ")
        set_time = input("Please, enter time for {}: ". format(task))
        calendar[task] = set_time
        print(calendar)


def conclusion():
    count = 0
    for i in range(0, num_task):
        is_done = input("Is task {} completed? Enter Yes/No: ".format(task))
        if is_done == "yes":
            count += 1
            return count
    print('Nicely done. {} of {} tasks were completed today'. format(count, num_task))

When I call conclusion, the input is_done is shown just once and the task is equal to the last task I typed in the function enter_task. Also the count is not counting and the statement is not printed. The input is_done should appear as many times as nun_task and each time with a different task on it.

python_newbie
  • 75
  • 1
  • 4
  • Why are `num_task` and `task` global? – meowgoesthedog Jan 24 '19 at 16:56
  • 6
    When you execute a `return` inside of a loop, the loop obviously does not continue to execute. – jasonharper Jan 24 '19 at 16:58
  • `return count` stops the loop at the first completed 'yes' task. You probably want it at the very end of conclusion? Also global variables is the root of many bugs. Should avoid them. – Demi-Lune Jan 24 '19 at 16:59
  • 1
    Change `for i in range(0, num_task):` to `for task in calendar:`. Read [iterating-over-dictionaries-using-for-loops](https://stackoverflow.com/questions/3294889/iterating-over-dictionaries-using-for-loops/3294899#3294899) – stovfl Jan 24 '19 at 17:11

0 Answers0