Click here to Skip to main content
15,889,858 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
I have two lists one called free hours and one called hoursbookedlist and I want to add them together to a list called total hours booked

#Imports Time
import random
import time

freehours = []
nameslist = []
hoursbookedlist = []
totalhoursbooked = []
for student in range(7):
name = input("Enter students name ")
hoursbooked = int(input("Enter number of hours booked for the students "))
while (hoursbooked <10):
print("Please re-enter number of hours booked: ")
hoursbooked = int(input("please the number of hours that have been booked for stundent1 "))
if hoursbooked > 10 and hoursbooked < 15:
freehours.append(1)
elif int(hoursbooked >=15):
freehours.append(2)

nameslist.append(name)
hoursbookedlist.append(hoursbooked)

totalhoursbooked = freehours + hoursbookedlist


print ("student \t hoursbooked \t freehours \t totalhoursbooked")
for i in range(7):
print(nameslist[i],"\t","\t",hoursbookedlist[i],"\t","\t",freehours[i],"\t","\t", totalhoursbooked[i])

What I have tried:

I tried just doing totalhoursbooked = freehourslist + hoursbookedlist
Posted
Updated 8-Jan-17 4:11am
Comments
Thomas Daniels 8-Jan-17 10:00am    
I'm not sure I understand what you ask... do you mean that you just want a new list with the same number of elements as freehourslist and hoursbookedlist, and each element in totalhoursbooked is the sum of the corresponding elements in freehourslist and hoursbookedlist? Like if you have [1, 1, 1, 1, 1, 1, 1] and [11, 12, 13, 14, 11, 12, 13], you want to have [12, 13, 14, 15, 12, 13, 14]?
Member 12939713 8-Jan-17 10:01am    
yeah exactly that

You already have an answer to this question at How do I get past LIST INDEX OUR of RANGE ERROR on Python[^]. Please do not repost; if you have information to add then edit your original question.
 
Share this answer
 
Comments
Thomas Daniels 8-Jan-17 9:55am    
It is actually another question... but then again I'm not so sure what he's asking here.
Member 12939713 8-Jan-17 9:56am    
how dont u understand how to I add the free hours and hours booked totherther so in the total hours booked list I have the answers
Richard MacCutchan 8-Jan-17 10:02am    
Do you mean add the two lists, or add the contents? If the latter then a simple loop will do it:
Hide   Copy Code
for i in range(len(freehourslist)):
    totalhoursbooked.append(freehourslist[i] + hoursbookedlist[i])

See Peter Leow's excellent solution below.
Member 12939713 8-Jan-17 10:03am    
ok but where in program does that go and at top do I need totalhoursbooked = []
Richard MacCutchan 8-Jan-17 10:12am    
Well it needs to go after the other two lists have been populated with values, and before you try to print its contents.
Try and adapt from this example:
list1 = [1,2,3,4]
list2 = [5,6,7,8]
list3 = [sum(x) for x in zip(list1, list2)]

Learn more about zip in Python[^]
 
Share this answer
 
v2
Comments
Member 12939713 8-Jan-17 10:05am    
thank you I managed
There are a few different ways to do that.

The simplest way is using a loop, as mentioned in Richard's comment.

You can also use operator.add and the map command:
Python
import operator
totalhoursbooked = list(map(operator.add, freehourslist, hoursbookedlist))
See the documentation of map[^]:
Quote:
Return an iterator that applies function to every item of iterable, yielding the results.
Then the list function transforms this iterator into a list.

You can also use a combination of list comprehension[^] and the zip function[^].
Python
totalhoursbooked = [a + b for a, b in zip(freehourslist, hoursbookedlist)]
Zip makes an iterator that aggregates elements from each of the iterables passed as arguments. (If you'd convert the zip return value to a list, you'd see a list of tuples where each tuple contains an element from freehourslist with the corresponding hoursbookedlist element).
 
Share this answer
 
v2
Comments
Peter Leow 8-Jan-17 10:20am    
This is more comprehensive, 5ed!
Thomas Daniels 8-Jan-17 10:21am    
Thank you.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900