the question is asking me to do this In a small highland village, everyone has the surname McIntosh, McGregor, McDonald or McKenzie.
Everyone is called Angus, Hamish, Morag or Mhairi
No two people have the same name.
Create a program to compile a list of the inhabitants of the village.
It makes use of lists and loops but I don’t know how to do it, this is what i have so far
surnames = ["Mcintosh", "McGregor", "McDonald", "Mckenzie"]
forenames = ["Angus", "Hamish", "Morag", "Mhairi"]
for forenames in forenames:
for surnames in surnames:
print forenames
>Solution :
You have the right idea with doing a nested loop, but you need to use a different variable name for the for var in iterable part, and you need to print the variables from both loops.
>>> surnames = ["Mcintosh", "McGregor", "McDonald", "Mckenzie"]
>>> forenames = ["Angus", "Hamish", "Morag", "Mhairi"]
>>> for fore in forenames:
... for sur in surnames:
... print(fore, sur)
...
Angus Mcintosh
Angus McGregor
Angus McDonald
Angus Mckenzie
Hamish Mcintosh
Hamish McGregor
Hamish McDonald
Hamish Mckenzie
Morag Mcintosh
Morag McGregor
Morag McDonald
Morag Mckenzie
Mhairi Mcintosh
Mhairi McGregor
Mhairi McDonald
Mhairi Mckenzie
Note that e.g. surnames is the list of all the surnames, and for sur in surnames assigns each individual surname to the variable sur.