A document from MCS 260 Fall 2021, instructor Emily Dumas. You can also get the notebook file.

MCS 260 Fall 2021 Homework 10

  • Course instructor: Emily Dumas


  • Complete the problems below, which ask you to write Python scripts.
  • Upload your python code directly to gradescope, i.e. upload the .py files containing your work. (If you upload a screenshot or other file format, you won't get credit.)


This homework assignment must be submitted in Gradescope by 10am CST on Tuesday, November 2, 2021.


This homework focuses on object-oriented programming. It is a bit shorter than usual to give you more time to work on Project 3.


Collaboration is prohibited, and you may only access resources (books, online, etc.) listed below.

Resources you may consult

The course materials you may refer to for this homework are:

(Lecture videos are not linked on worksheets, but are also useful to review while working on worksheets. Video links can be found in the course course Blackboard site.)

Point distribution

This homework assignment has 2 problems, numbered 2 and 3. Problem 2 is a bit longer than usual, so it gets 8 points this time. Thus the grading breakdown is:

Points Item
2 Autograder
6 Problem 2
8 Total

What to do if you're stuck

Ask your instructor or TA a question by email, in office hours, or on discord.

( 1. There's no problem 1 )

Gradescope will show the results of the automated syntax check of all submitted files as the score for problem 1.

2. Chemical element class

Create a file hwk10prob2.py that contains a single class definition, for a class Element. Objects in this class will store chemical elements. The class should have the following behavior:

  • The constructor has three required arguments other than self. They are:
    • Z, the atomic number of the element (a positive integer that uniquely identifies it), e.g. 3
    • name, the full name of the element, e.g. Lithium
    • symbol, the one- or two-letter symbol used to represent the element, e.g. Li
  • The constructor stores its arguments as attributes of self, so e.g. an Element object e has attributes e.Z, e.name, and e.symbol.
  • The __str__ method returns a string representation in this format: Lithium (#3). (It doesn't print anything, it just returns the relevant string.)
  • The __repr__ method returns the same thing as __str__. (It doesn't print anything, it just returns the relevant string.)
  • The == operator is overloaded by a method __eq__ of this class, which checks whether the object this element is being compared to has the same atomic number, returning a boolean. If the other object has a different atomic number, or if it isn't an instance of class Element, then this method should return False.

Below is some test code that should work once you've defined this class, and the expected output is shown below it. Don't include this code in your submission. Use it to test your work, but then only submit hwk10prob2.py containing a single class definition.

Make sure there are docstrings at the file, class, and method level.

In [10]:
import hwk10prob2

a = hwk10prob2.Element(Z=47,name="Silver",symbol="Ag")
b = hwk10prob2.Element(Z=47,name="Silber",symbol="Ag") # German name
c = hwk10prob2.Element(19,"Potassium","K") # pass arguments positionally instead of using keywords

print("Testing Element.__str__:")
print("\nTesting Element.__repr__:")
print("\nTesting existence of attributes:")
print("Z =",a.Z)
print("name =",a.name)
print("symbol =",a.symbol)
print("\nTesting equality:")
print("In a case with expected result True:",a==b) # comparison should consider only Z, ignoring name and symbol 
print("In a case with expected result False:",a==c)
print("In a case with expected result False:",a=="whatever")
Testing Element.__str__:
Silver (#47)
Potassium (#19)

Testing Element.__repr__:
Silver (#47)
Potassium (#19)

Testing existence of attributes:
Z = 47
name = Silver
symbol = Ag

Testing equality:
In a case with expected result True: True
In a case with expected result False: False
In a case with expected result False: False

Revision history

  • 2021-10-28 Initial release