To start this assignment, download this zip file.
Homework 3g - Structured Data
Here are some useful reminders about errors
PyCharm will attempt to catch errors before you actually run the program. If you see a red squiggly line underneath your code, this indicates a critical error that will cause your program to crash. In this example, the error is a result of using =
instead of ==
to compare.
PyCharm also has a yellow highlight, which indicates that there is likely a logical error in your code. Your code will probably run without crashing, but will probably produce the incorrect result. In this example, PyCharm has produced a yellow underline because the get_vegetables()
function doesn’t do anything.
Green underlines indicate misspelling.
Gray underlines indicate a formatting issue (running the “Format Code” tool in PyCharm should fix these, see Run the PyCharm Formatter).
In this homework assignment, you will be given multiple pieces of code with a variety of bugs. Fix each of the bugs and run the tests to ensure it works properly. You should also use the debugger when necessary.
1. Calculator
The calculator.py
file already has most of the necessary code, but has some type of error or bug. Run the code to identify what the bug is, then fix it. The correct code should result in something that looks like this.
What would you like to do?
1) Add
2) Subtract
3) Quit
Option: 2
Number 1: 16
Number 2: 7
9
What would you like to do?
1) Add
2) Subtract
3) Quit
Option: 1
Number 1: 91
Number 2: 22
113
What would you like to do?
1) Add
2) Subtract
3) Quit
Option: help
Unrecognized response: help
What would you like to do?
1) Add
2) Subtract
3) Quit
Option: 3
You should run the tests to make sure the tests pass. Keep in mind that some errors may not crash the program but may fail the tests. To view the original instructions for this assignment, see homework 3b.
2. Words
The words.py
file has most of the necessary code already written. Run the code, identify the bugs, and then fix them. Keep in mind that some bugs may not cause the program to crash but will cause the tests to fail.
When correctly working, the code should result in something like this:
Enter a secret word: python
Guess a word: slate
Lower!
Guess a word: crane
Higher!
Guess a word: pickles
Higher!
Guess a word: quiz
Lower!
Guess a word: poodle
Higher!
Guess a word: python
You got it!
To view the original instructions for this assignment, see homework 3b.
3. Compare Lists
The compare_lists.py
file has mostly complete code. Run the code and the test to find, identify, and fix any bugs in the code. Keep in mind some bugs do not cause the program to crash but will cause the tests to fail.
When the code is complete, the output should look something like this:
Enter a Fruit: apple
Enter a Fruit:
Enter a Vegetable: carrot
Enter a Vegetable: squash
Enter a Vegetable:
Vegetables:
- carrot
- squash
Fruits:
- apple
You need more fruit!
To view the original instructions for this assignment, see homework 3c.
4. Student Ratings
The student_ratings.py
file has mostly complete code. Run the code and the tests, find the bugs, and fix them.
When the code is complete, the output should look like this:
Enter ratings for this class.
Each rating includes a score and a comment.
Use a blank score to end.
Score: 9.5
Comment: Great class!
Score: 8.3
Comment: Good, but too much homework.
Score: 9.2
Comment: I like the teacher but don't like the material much.
Score: 7.1
Comment: I couldn't stay awake.
Score: 8.8
Comment: I learned a lot because the TAs were awesome.
Score:
Average rating: 8.6
Comments:
- Great class!
- Good, but too much homework.
- I like the teacher but don't like the material much.
- I couldn't stay awake.
- I learned a lot because the TAs were awesome.
To view the original instructions for this assignment, see homework 3e.
Tests
Be sure that the code passes the tests provided in the assignment. Review the guide on using pytest if you need to review using pytest and what to do if a test fails.