Practice - 5 Tasks
Answer the questions below
1fill in blank
easyComplete the code to create a simple bar chart showing rankings.
Matplotlib
import matplotlib.pyplot as plt items = ['A', 'B', 'C', 'D'] scores = [4, 2, 3, 1] plt.bar(items, [1]) plt.show()
Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Using the item names instead of scores for bar heights.
Passing a range object instead of a list of scores.
✗ Incorrect
The bar chart needs the heights of bars, which are the scores.
2fill in blank
mediumComplete the code to sort the items by their scores before plotting.
Matplotlib
items = ['A', 'B', 'C', 'D'] scores = [4, 2, 3, 1] sorted_pairs = sorted(zip(items, scores), key=lambda x: x[[1]]) sorted_items, sorted_scores = zip(*sorted_pairs) import matplotlib.pyplot as plt plt.bar(sorted_items, sorted_scores) plt.show()
Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Sorting by index 0 which is the item name, not the score.
Using an invalid index like 2 which does not exist.
✗ Incorrect
The scores are the second element in each pair, index 1.
3fill in blank
hardFix the error in the code to display the ranking chart with labels rotated.
Matplotlib
import matplotlib.pyplot as plt items = ['A', 'B', 'C', 'D'] scores = [4, 2, 3, 1] plt.bar(items, scores) plt.xticks(rotation=[1]) plt.show()
Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Passing rotation as a string causes an error.
Using an invalid keyword inside plt.xticks.
✗ Incorrect
The rotation argument expects an integer number of degrees, not a string.
4fill in blank
hardFill both blanks to create a horizontal bar chart sorted by scores.
Matplotlib
import matplotlib.pyplot as plt items = ['A', 'B', 'C', 'D'] scores = [4, 2, 3, 1] sorted_pairs = sorted(zip(items, scores), key=lambda x: x[[1]]) sorted_items, sorted_scores = zip(*sorted_pairs) plt.[2](sorted_items, sorted_scores) plt.show()
Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Sorting by index 0 which is item name.
Using plt.bar instead of plt.barh for horizontal bars.
✗ Incorrect
Sort by scores at index 1 and use plt.barh for horizontal bars.
5fill in blank
hardFill all three blanks to create a ranking chart with colors based on score thresholds.
Matplotlib
import matplotlib.pyplot as plt items = ['A', 'B', 'C', 'D'] scores = [4, 2, 3, 1] colors = ['green' if score [1] 3 else 'red' for score in scores] plt.bar(items, scores, color=[2]) plt.title('Ranking Chart') plt.ylabel([3]) plt.show()
Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Using '<' instead of '>' changes color logic.
Passing a string instead of the colors list to color.
Not labeling the y-axis properly.
✗ Incorrect
Use '>' to check scores above 3, pass colors list, and label y-axis as 'Scores'.