Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

University Question including for loops and lists

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

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

>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.

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading