34.0k views
1 vote
Peyton is taking a part-time job to earn some extra money. Every week the manager will provide a list of tasks and the number of hours each task requires. The tasks have to be done one by one from the first one and Peyton would do so until the time spent on all finished tasks exceeds 10 hours. Please construct a function take_tasks, which takes one integer list as the argument task_hours and prints out how many hours and how many tasks Peyton would do for that week. For example, the program below

tasks ([2, 1, 3, 1, 4, 2, 3])
will have the following output:
Finish 5 tasks in 11 hours
Note: you can use enumerate() to get the index of the task, which is available after the for loop terminates. However, keep in mind that index starts from 0.
time_spent = 0
time_spent = 10
if time_spent > 10: break
if time_spent > 0: break
for index, hour in task_hours:
print('Finish {} tasks in {} he
if time_spent == 10: break
def take_tasks (task_hours):
time_spent += index
time_spent += hour
for index, hour in enumerate

1 Answer

4 votes

Answer:

The function is as follows:

def tasks(Petyontasks):

timespent = 0

for i, tsk in enumerate(Petyontasks):

if timespent<=10:

timespent+=tsk

else:

break

print("Finish "+str(i)+" tasks in "+str(timespent)+" hours")

Step-by-step explanation:

The program in the question cannot be traced. Hence, the need to begin from scratch.

This defines the function

def tasks(Petyontasks):

This initializes timespent to 0

timespent = 0

This iterates through the tasks [i represents the count of the tasks while tsk represents the time on each task]

for i, tsk in enumerate(Petyontasks):

If timespent is less or equal to 10

if timespent<=10:

The tasks is added and the timespent is calculated

timespent+=tsk

If otherwise

else:

The loop is exited

break

This prints the required output

print("Finish "+str(i)+" tasks in "+str(timespent)+" hours")

User Vijay Tholpadi
by
4.5k points