Hack Forums
[Python] Doing Mistakes - Printable Version

+- Hack Forums (https://hackforums.net)
+-- Forum: Tech (https://hackforums.net/forumdisplay.php?fid=444)
+--- Forum: Coding (https://hackforums.net/forumdisplay.php?fid=151)
+---- Forum: Software Development (https://hackforums.net/forumdisplay.php?fid=118)
+---- Thread: [Python] Doing Mistakes (/showthread.php?tid=5870618)



Doing Mistakes - CyberError - Aug 6, 2018

hey guys so am learning Python from codeacadymy and other few resourse , but while practing am keep repating spaces mistake , i do i know where to put "spaces" and where i should add ":"

ex

like in this code

def fizz_count(x):
 count = 0
 for item in x:
   if item == "fizz":          #i forgot to add semicolon here
     count = count + 1
   return count


RE: Doing Mistakes - lowfatcheeze - Aug 6, 2018

According to PEP8, which is a document that explains how to properly style your python code, you're supposed to add 4 spaces before lines that should be nested. In all of your lines after 'def fizz_count(x):' you only have one space. You should use 4 spaces instead. It's acceptable, though not reccommended, to used a tab for in place of the 4 spaces (but please don't do this, it's a bad habit!).

This is how your code should be properly formatted:
Code
def fizz_count(x):
   count = 0
   for item in x:
        if item == "fizz":          #i forgot to add semicolon here
            count = count + 1
   return count

[PS] If x is a list ( type(x) == 'list' ), then you can simplify your code to:
x.count('fizz')


RE: Doing Mistakes - CyberError - Aug 6, 2018

(Aug 6, 2018 06:46 PM)lowfatcheeze Wrote:  According to PEP8, which is a document that explains how to properly style your python code, you're supposed to add 4 spaces before lines that should be nested. In all of your lines after 'def fizz_count(x):' you only have one space. You should use 4 spaces instead. It's acceptable, though not reccommended, to used a tab for in place of the 4 spaces (but please don't do this, it's a bad habit!).

This is how your code should be properly formatted:

def fizz_count(x):
   count = 0
   for item in x:
       if item == "fizz":          #i forgot to add semicolon here
           count = count + 1
   return count


[PS] If x is a list ( type(x) == 'list' ), then you can simplify your code to:
x.count('fizz')

You telling me to add 4 spaces but you yourself removed every single space


RE: Doing Mistakes - lowfatcheeze - Aug 6, 2018

(Aug 6, 2018 06:46 PM)lowfatcheeze Wrote:  According to PEP8, which is a document that explains how to properly style your python code, you're supposed to add 4 spaces before lines that should be nested. In all of your lines after 'def fizz_count(x):' you only have one space. You should use 4 spaces instead. It's acceptable, though not reccommended, to used a tab for in place of the 4 spaces (but please don't do this, it's a bad habit!).

This is how your code should be properly formatted:

def fizz_count(x):
   count = 0
   for item in x:
       if item == "fizz":          #i forgot to add semicolon here
           count = count + 1
   return count


[PS] If x is a list ( type(x) == 'list' ), then you can simplify your code to:
x.count('fizz')

(Aug 6, 2018 07:05 PM)CyberError Wrote:  You telling me to add 4 spaces but you yourself removed every single space

You're supposed to. I'm not sure why no spaces are showing in my original post, yet your quote of my post has the spaces.


RE: Doing Mistakes - CyberError - Aug 6, 2018

(Aug 6, 2018 07:23 PM)lowfatcheeze Wrote:  You're supposed to. I'm not sure why no spaces are showing in my original post, yet your quote of my post has the spaces.

Oh yeah , thanks for the reply
Is their any guide or topic so I can read about spaces ? With suitable examples


RE: Doing Mistakes - lowfatcheeze - Aug 6, 2018

(Aug 6, 2018 07:41 PM)CyberError Wrote:  Oh yeah , thanks for the reply
Is their any guide or topic so I can read about spaces ? With suitable examples

I don't know of a good guide for them, but the concept is called "nesting." You might want to google something like "python nesting" and see if there's anything helpful for you. Let me know if you can't get it after some time researching.


RE: Doing Mistakes - CyberError - Aug 6, 2018

(Aug 6, 2018 09:03 PM)lowfatcheeze Wrote:  I don't know of a good guide for them, but the concept is called "nesting." You might want to google something like "python nesting" and see if there's anything helpful for you. Let me know if you can't get it after some time researching.

when i searched for python nesting i found "python looping: tut


anyways thanks i found something , when i searched spaces in python

http://www.annedawson.net/Python_Spaces_Indentation.html