61.2k views
3 votes
The line above creates a dictionary called gradebook with six keys (all strings) and six values (all strings). What of the following lines, if run after the line above has been executed, will not cause an error? Select all that apply.

a) gradebook7 = "B"
b) gradebook.update("John": "A")
c) del gradebook"Alice"
d) gradebook.pop("Bob")

1 Answer

7 votes

Final answer:

The lines of code that do not cause an error with the Python dictionary called gradebook are gradebook['Alice'] = 'B' and gradebook.pop('Bob'). The other lines either cause a syntax error or are using incorrect method syntax.

Step-by-step explanation:

The student is asking about manipulating a Python dictionary called gradebook. From the options given, the lines that will not cause an error are:

  • gradebook['Alice'] = 'B': Assigns the value 'B' to the key 'Alice' in the dictionary.
  • gradebook.pop('Bob'): Removes the key 'Bob' and its associated value from the dictionary.

The remaining options contain syntax errors or incorrect Python methods usage:

  • gradebook7 = 'B': This line does not cause a syntax error, but it does not interact with the gradebook dictionary. It simply assigns the string 'B' to a new variable called gradebook7.
  • gradebook.update('John': 'A'): The correct syntax for the update method should use braces for a dictionary argument gradebook.update({'John': 'A'}).
  • del gradebook['Alice']: This should be formatted as del gradebook['Alice'] with square brackets to delete the key 'Alice' from the dictionary.

User Luca Spiller
by
8.7k points