Skip to content

programming_fundamentals/python_part_1/example3.py question #103

Description

@gregbo

Instead of

message += "The color you added was {}.".format(colors[3])

why not use

message += "The color you added was {}.".format(colors[-1])

so it will always pick up the last list element even if the number of list elements changes?

It would be even better to catch an exception if the list is empty.

try:
message += "The color you added was {}.".format(colors[-1])
except Exception as e:
print(e)
exit(1)

Activity

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions