{"id":421,"date":"2023-08-27T13:56:14","date_gmt":"2023-08-27T13:56:14","guid":{"rendered":"https:\/\/python.garden\/?p=421"},"modified":"2023-08-27T14:15:10","modified_gmt":"2023-08-27T14:15:10","slug":"find-the-pep-errors","status":"publish","type":"post","link":"https:\/\/python.garden\/index.php\/2023\/08\/27\/find-the-pep-errors\/","title":{"rendered":"Find the PEP errors:"},"content":{"rendered":"\n<p class=\"wp-block-paragraph\">The following code has 10 PEP8 Violations, can you find them all?<br>Code Sample with PEP 8 Violations<\/p>\n\n\n\n<div class=\"wp-block-codemirror-blocks-code-block code-block\"><pre class=\"CodeMirror\" data-setting=\"{&quot;showPanel&quot;:true,&quot;languageLabel&quot;:&quot;language&quot;,&quot;fullScreenButton&quot;:true,&quot;copyButton&quot;:true,&quot;mode&quot;:&quot;python&quot;,&quot;mime&quot;:&quot;text\/x-python&quot;,&quot;theme&quot;:&quot;material&quot;,&quot;lineNumbers&quot;:true,&quot;styleActiveLine&quot;:true,&quot;lineWrapping&quot;:false,&quot;readOnly&quot;:false,&quot;fileName&quot;:&quot;&quot;,&quot;language&quot;:&quot;Python&quot;,&quot;maxHeight&quot;:&quot;400px&quot;,&quot;modeName&quot;:&quot;python&quot;}\">import math,sys;\n\ndef example1(x,y): return x+y\n\ndef example2(x,y): \n  print(x+y)\n\nclass myClass:\n  def __init__(self,r):self.radius=r\n  def calc_area(self):\n    return math.pi*self.radius**2\n\ndef run():\n    print(&quot;Hello, world!&quot;)\n    print(example1(1,2))\n    example2(3, 4)\n    C=myClass(5)\n    print(&quot;Area is:&quot;,C.calc_area())\n\nif __name__== &quot;__main__&quot;:run()<\/pre><\/div>\n\n\n\n<details class=\"wp-block-mamaduka-toggles wp-block-toggles\"><summary>Answer Key<\/summary><div class=\"wp-block-toggles__content\"><div class=\"wp-block-ub-expand ub-expand\" id=\"ub-expand-73ba9886-992c-43ca-9a1a-8cdb7a968245\" data-scroll-type=\"false\" data-scroll-amount=\"\" data-scroll-target=\"\">\n<div class=\"ub-expand-portion ub-expand-partial wp-block-ub-expand-portion\" id=\"ub-expand-partial-73ba9886-992c-43ca-9a1a-8cdb7a968245\" aria-hidden=\"false\">\n\t\t\t\n\n<ol class=\"wp-block-list\">\n<li><strong>Import Statements<\/strong>: <code>import math,sys;<\/code> should be split into separate lines.\n<ul class=\"wp-block-list\">\n<li>Corrected:pythonCopy code<code>import math import sys<\/code><\/li>\n<\/ul>\n<\/li>\n\n\n\n<li><strong>Semicolon<\/strong>: Semicolon at the end of the import statements is unnecessary.\n<ul class=\"wp-block-list\">\n<li>Corrected: Remove the semicolon.<\/li>\n<\/ul>\n<\/li>\n\n\n\n<li><strong>Function Naming<\/strong>: Function names should be lowercase with underscores (<code>example1<\/code> and <code>example2<\/code> are fine, but <code>myClass<\/code> and <code>calc_area<\/code> are not consistent).\n<ul class=\"wp-block-list\">\n<li>Corrected:pythonCopy code<code>class MyClass:<\/code><\/li>\n<\/ul>\n<\/li>\n\n\n\n<li><strong>Function Definition<\/strong>: <code>def example1(x,y): return x+y<\/code> is on a single line.\n<ul class=\"wp-block-list\">\n<li>Corrected:pythonCopy code<code>def example1(x, y): return x + y<\/code><\/li>\n<\/ul>\n<\/li>\n\n\n\n<li><strong>Whitespace<\/strong>: Missing whitespace after commas in function arguments.\n<ul class=\"wp-block-list\">\n<li>Corrected: Add space after each comma.<\/li>\n<\/ul>\n<\/li>\n\n\n\n<li><strong>Whitespace in Function<\/strong>: <code>def __init__(self,r):self.radius=r<\/code> has no spaces.\n<ul class=\"wp-block-list\">\n<li>Corrected:pythonCopy code<code>def __init__(self, r): self.radius = r<\/code><\/li>\n<\/ul>\n<\/li>\n\n\n\n<li><strong>Operators<\/strong>: <code>self.radius**2<\/code> lacks whitespace around the operator.\n<ul class=\"wp-block-list\">\n<li>Corrected: <code>self.radius ** 2<\/code><\/li>\n<\/ul>\n<\/li>\n\n\n\n<li><strong>Indentation<\/strong>: Inconsistent indentation (use 4 spaces).\n<ul class=\"wp-block-list\">\n<li>Corrected: Use 4 spaces for indentation.<\/li>\n<\/ul>\n<\/li>\n\n\n\n<li><strong>Double Equality in If Statement<\/strong>: Use a single space around the <code>==<\/code> operator.\n<ul class=\"wp-block-list\">\n<li>Corrected:pythonCopy code<code>if __name__ == \"__main__\":<\/code><\/li>\n<\/ul>\n<\/li>\n\n\n\n<li><strong>Whitespace After Print<\/strong>: Remove unnecessary space after print function.\n<ul class=\"wp-block-list\">\n<li>Corrected:pythonCopy code<code>print(\"Area is:\", C.calc_area())<\/code><\/li>\n<\/ul>\n<\/li>\n<\/ol>\n\n\n\n<p class=\"wp-block-paragraph\"><\/p>\n\n\n\t\t\t<a id=\"ub-expand-toggle-partial-73ba9886-992c-43ca-9a1a-8cdb7a968245\" class=\"ub-expand-toggle-button\" style=\"text-align: left; \" role=\"button\" aria-expanded=\"false\" aria-controls=\"ub-expand-full-73ba9886-992c-43ca-9a1a-8cdb7a968245\" tabindex=\"0\">\n\t\t\t\t\n\t\t\t<\/a>\n\t\t<\/div>\n\n<div class=\"ub-expand-portion ub-expand-full ub-hide wp-block-ub-expand-portion\" id=\"ub-expand-full-73ba9886-992c-43ca-9a1a-8cdb7a968245\" aria-hidden=\"true\">\n\t\t\t\n\t\t\t<a id=\"ub-expand-toggle-full-73ba9886-992c-43ca-9a1a-8cdb7a968245\" class=\"ub-expand-toggle-button\" style=\"text-align: left; \" role=\"button\" aria-expanded=\"true\" aria-controls=\"ub-expand-full-73ba9886-992c-43ca-9a1a-8cdb7a968245\" tabindex=\"0\">\n\t\t\t\t\n\t\t\t<\/a>\n\t\t<\/div>\n<\/div><\/div><\/details>\n\n\n\n<p class=\"wp-block-paragraph\"><\/p>\n","protected":false},"excerpt":{"rendered":"<p>The following code has 10 PEP8 Violations, can you find them all?Code Sample with PEP 8 Violations<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"_jetpack_memberships_contains_paid_content":false,"footnotes":""},"categories":[1],"tags":[],"class_list":["post-421","post","type-post","status-publish","format-standard","hentry","category-uncategorized"],"featured_image_src":null,"author_info":{"display_name":"shababdoo","author_link":"https:\/\/python.garden\/index.php\/author\/shababdoo\/"},"jetpack_featured_media_url":"","jetpack_sharing_enabled":true,"_links":{"self":[{"href":"https:\/\/python.garden\/index.php\/wp-json\/wp\/v2\/posts\/421","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/python.garden\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/python.garden\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/python.garden\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/python.garden\/index.php\/wp-json\/wp\/v2\/comments?post=421"}],"version-history":[{"count":0,"href":"https:\/\/python.garden\/index.php\/wp-json\/wp\/v2\/posts\/421\/revisions"}],"wp:attachment":[{"href":"https:\/\/python.garden\/index.php\/wp-json\/wp\/v2\/media?parent=421"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/python.garden\/index.php\/wp-json\/wp\/v2\/categories?post=421"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/python.garden\/index.php\/wp-json\/wp\/v2\/tags?post=421"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}