â€¢ Use inline comments to describe complex sections of code Avoid unnecessary and trivial comments
avg = (score1 + score2 + score3)/3 # add scores and divide by 3_x000D_
ALMOST AS BAD – because well designed variable names should make the comment unnecessary
avg = (score1 + score2 + score3)/3 # calculate average_x000D_
Whitespace and indentation:
â€¢ Use correct, consistent indentation.
o All blocks of code within a given function or control flow structure must be distinguished
from the preceding code by one indentation level.
o Use 4 spaces per indentation level. IDLE will automatically indent 4 spaces when you hit
the tab key.
- Always place a line break after a colon ( : )
- Avoid long lines.
o Avoid lines longer than 80 characters, including indentation. Make sure to maintain
correct indentation when breaking long lines into shorter ones.
- Make smart variable type decisions and utilize type conversionsStore data using the appropriate data types (making sure to use the appropriate literals as well), and make sure to use the type conversion functions (str(), int(), float())when necessary. Remember, for example, that if you ask the user to input the temperature outside and you want to use that value in a calculation, you will have to convert their input to a float before you can use it.
- Follow variable naming conventions
Name variables using underscore (AKA snake case) like_this. Use concise, descriptive namesfor variables that precisely describe what information they’re storing.
- Create one variable per line of codeNever initialize more than one variable in a single statement.# bad # good a, b, c = 7, -43, 19 a = 7b = -43 c = 19
â€¢ Use named constants when appropriate
If a particular constant value is used frequently in your code, identify it as a constant, and always refer to the constant in the rest of your code rather than referring to the corresponding literal value. Name constants in uppercase with underscores between words LIKE_THIS.
DAYS_IN_WEEK = 7_x000D_ DAYS_IN_YEAR = 365_x000D_ HOURS_IN_DAY = 24_x000D_
Part 2 â€“ Conditionals and Loops
These requirements apply to Chapters 3 & 4 and later assignments
â€¢ Avoid empty if or else branches
o When using if/else statements, you should not have `if` or `else` branches that are blank.
Rephrase your condition to avoid this.
â€¢ Avoid unnecessary control flow checks
When using if/else statements, properly choose between various if and else patterns depending on whether the conditions are related to each other. Avoid redundant or unnecessary if tests. Ask yourself if all of the conditions always need to be checked.
# bad_x000D_ if points >= 90:_x000D_
print('You got Gold!')_x000D_ if points >= 70 and points <_x000D_ 90:_x000D_
print('You got Silver!')_x000D_ if points >= 50 and points <_x000D_ 70:_x000D_
print('You got Bronze!')_x000D_ ..._x000D_
# good_x000D_ if (points >= 90):_x000D_
print(‘You got Gold!’)} elif points >= 70:
print(‘You got Silver!’) elif points >= 50:
print('You got Bronze!')_x000D_ ..._x000D_
- Beware of infinite loops
Avoid writing loops that never stop. An infinite loop will cause your program to neverstop executing. Replace infinite loops with loops that terminate.
- Choose the right loopConsider when it would be more appropriate to use a while loop or for item in list loop or for item in range loop.
Part 3 â€“ Functions
These requirements apply to Chapter 5 and subsequent assignments
- Use descriptive names for functionsGive functions descriptive names, such as discount(price, rate) or make_a_star(). Avoid one-letter names and non-descriptive names, like x() or go() or function1().Function names should be all lowercase, with words separated by underscores to improve readability.
- Keep your main program a concise summaryAs much as possible, avoid having too much of your program’s functionality directly in your main() code. When writing your programs, try to break the problem down into smaller sub-problems. Create functions for these individual sub-problems. This makes your program easy to read and forms a concise summary of the overall behavior of the program.
- Minimize redundant codeIf you repeat the same code block two or more times, find a way to remove the redundant code so that it appears only once. For example, you can place it into a function that is called from both places.
- Avoid long functionsEach function should perform a single, clear, coherent task. If you have a single function that is very long or that is doing too much of the overall work, break it into smaller sub- functions. If you try to describe the function’s purpose and find yourself using the word “and” a lot, that probably means the function is doing too many things and should be split into sub-functions.
- Consider short functions for common tasks
It can be useful to have a short function if it wraps another function with some additionalcontrol flow or if it contributes to an easier to understand name.# bad
def square_root(x):_x000D_ return math.sqrt(x)_x000D_
def discount(price, rate):_x000D_ discount = price â€“ rate*price_x000D_
- Write an overall header comment for every function – use the IPO notation described in Chapter 5 slide 49
â€¢ Eliminate redundancy by introducing functions with parameters
If you repeat the same code two or more times that is nearly but not entirely the same, try
making a helper function that accepts a parameter to represent the differing part.
x = foo(10) y= x- 1 print(x * y) …
x = foo(15) y= x- 1 print(x * y)
print(helper(10))_x000D_ print(helper(15))_x000D_ ..._x000D_
def helper(p):_x000D_ x = foo(p)_x000D_
y=x -1 return x * yx
Part 4 â€“ Classes
These requirements apply to Chapter 10 and subsequent assignments
- Do not access object attributes directly in the client (abstraction)You should avoid directly accessing attributes of classes you write. Instead, use getter and setter methods to access the appropriate pieces of information when necessary.
- Keep related tasks bundled together within the appropriate class (encapsulation)You should design your classes in a such a way that all of the appropriate data and the methods that act upon that data are all encapsulated within the class. For example, the client of a graphics program should not be implementing its own graphical behavior or directly accessing graphical objects.
- Avoid unnecessary instance variables (attributes)Use instance variables (attributes) only when they are required to implement certain functionality, or if it makes sense for that variable to persist outside of any given method. Don’t use them to store temporary values used in only a single call to one method, for instance. You should assess whether a variable is needed across multiple methods before making it an instance variable.
Focus on dictionary methods, use of functions, and good programming style
For this assignment, you will create a glossary (dictionary) of technical terms and definitions. It will be set up as a Python dictionary structure. The file assign08_starter.py is a complete starter framework for the assignment. It includes some initial values for the dictionary. IT is long because most of the code has already been written for you.
Your task is to complete the five individual functions for adding and deleting terms, looking up terms, listing them, and printing out both the terms and definitions. These functions are all short, just a couple of lines, and use basic dictionary methods and techniques.
Here is some sample output.
CSIT 512 Elements of Computer Programming Graded Programming Assignment 8
Notice that for 2 and 5, the terms are listed in alphabetical order. Also, lookup and delete should not be case sensitive.