02 Exercise: PEP 8 Violation Spotting
Instructions
This interactive quiz tests your ability to spot PEP 8 violations in Python code. The questions are based on the Level 2 PEP 8 exercises and cover:
- Naming conventions (snake_case, PascalCase, UPPER_CASE)
- Whitespace rules (spaces around operators, after commas, after colons)
- Import organization (one import per line, removing unused imports)
- Compound statements (avoid multiple statements on one line)
- Line length (79-88 character limit)
- Resource management (using
withfor file handling)
Read each code snippet carefully and try to identify all the violations before selecting your answer. Good luck!
How many PEP 8 violations are in this code?
import sys,os
import numpy as np
def ProcessData(input_list,threshold=10):
result=[]
for item in input_list:
if item>threshold:result.append(item*2)
return result
Select ALL PEP 8 violations present in this code:
import sys,os
import numpy as np
def ProcessData(input_list,threshold=10):
result=[]
for item in input_list:
if item>threshold:result.append(item*2)
return result
What naming convention should the function ProcessData use according to PEP 8?
How many PEP 8 violations are in this code?
from dash import Dash,html,dcc
import numpy as np
import sys
def generate_road_profile(num_points=100,x_max=80):
x=np.linspace(0,x_max,num_points)
y=0.015*x**3+0.3*np.sin(2*np.pi*x)
return x,y
camera_position={'x':0,'y':2.0,'z':1.5}
MAX_distance=100
Select ALL PEP 8 violations present in this code snippet:
camera_position={'x':0,'y':2.0,'z':1.5}
MAX_distance=100
What is wrong with the variable name MAX_distance according to PEP 8?
Select ALL PEP 8 violations present in this code:
import numpy as np
from typing import List
def HelperFunction(val):
result=val*2
return result
def another_helper(x,y,z): return x+y+z
def calculate_distance(point1,point2):
dx=point2[0]-point1[0]
dy=point2[1]-point1[1]
return np.sqrt(dx**2+dy**2)
# This is a very long comment that definitely exceeds the recommended 79 character limit for code readability
Why does PEP 8 discourage the compound statement def another_helper(x,y,z): return x+y+z?
Select ALL PEP 8 violations present in this code:
import sys,os,json
class DataProcessor:
def __init__(self,data,config=None):
self.data=data
self.config=config if config else {}
def Process(self,threshold=0):
filtered=[x for x in self.data if x>threshold]
return filtered
def GetStats(self):
return {'mean':np.mean(self.data),'std':np.std(self.data)}
max_items=1000
According to PEP 8, what naming convention should the methods Process and GetStats use?
This code has a bonus issue that is not strictly a PEP 8 violation but would cause a runtime error. What is it?
import sys,os,json
class DataProcessor:
def __init__(self,data,config=None):
self.data=data
self.config=config if config else {}
def GetStats(self):
return {'mean':np.mean(self.data),'std':np.std(self.data)}
Select ALL PEP 8 violations present in this code:
from typing import List,Tuple
import numpy as np
def find_intersection(x_road,y_road,angle_degrees,camera_x=0,camera_y=1.5):
angle_rad=np.radians(angle_degrees)
ray_dx=np.cos(angle_rad)
ray_dy=np.sin(angle_rad)
for i in range(len(x_road)-1):
x1,y1=x_road[i],y_road[i]
x2,y2=x_road[i+1],y_road[i+1]
t=((camera_y-y1)*ray_dx-(camera_x-x1)*ray_dy)/((x2-x1)*ray_dy-(y2-y1)*ray_dx)
if 0<=t<=1:
x_intersect=x1+t*(x2-x1)
return x_intersect,y1+t*(y2-y1)
return None,None
The following line from Exercise 2.5 is too long. What is the BEST way to fix it according to PEP 8 best practices?
t=((camera_y-y1)*ray_dx-(camera_x-x1)*ray_dy)/((x2-x1)*ray_dy-(y2-y1)*ray_dx)
Select ALL PEP 8 violations present in this code:
import json,sys
from pathlib import Path
class ConfigManager:
def __init__(self,config_file="config.json"):
self.config_file=config_file
self.data=self.load_config()
def load_config(self):
if Path(self.config_file).exists():return json.load(open(self.config_file))
return {}
def get_value(self,key,default=None):
return self.data.get(key,default)
def update_config(self,updates):
self.data.update(updates)
with open(self.config_file,"w") as f:json.dump(self.data,f)
DEFAULT_timeout=30
max_retries=3
What is the correct way to rewrite this compound statement from Exercise 2.6?
if Path(self.config_file).exists():return json.load(open(self.config_file))
How should the following module-level variables be renamed according to PEP 8?
DEFAULT_timeout=30
max_retries=3