Created
July 9, 2020 08:37
-
-
Save Dan6erbond/948246b1899d4cfbc46b88c8f3018cd5 to your computer and use it in GitHub Desktop.
A Python exercise focused on multidimensional lists.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
def main(): | |
print("Input the folowing data for the donor:") | |
donors = list() # 'columns represent name, address, contact' | |
# get information for three donors | |
for i in range(3): | |
# ask for donor information and store in temporary variables | |
first_name = input('First name: ') | |
address = input('Address: ') | |
contact = input('Contact: ') | |
# append a new list with the retrieved information to the donor list | |
donors.append([first_name, address, contact]) | |
for i in range(3): # iterate over all the rows | |
# store the donor's credentials in variables given we know the cell index | |
first_name = donors[i][0] | |
address = donors[i][1] | |
contact = donors[i][2] | |
# neatly print out the summary | |
print(f"First name: {first_name} Address: {address} Contact: {contact}") | |
# main() | |
donors = list() # create an empty list | |
for i in range(3): # perform this loop three times | |
donor = ["John", "San Francisco", "Email"] # create a new list with the donor's information | |
donors.append(donor) # add it as a sublist to the donor list | |
print(donors) # print the new donor list | |
for i in range(len(donors)): # one option is iterating over indices | |
donor = donors[i] # store the current sublist based on the indice into a variable | |
print(donor) # print the donor | |
for donor in donors: # but you can also iterate over the rows directly | |
print(donor) # print the donor |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment