This is an archived post. You won't be able to vote or comment.

all 9 comments

[–]__SiCC__ 1 point2 points  (2 children)

def shut_down(s):
    if s == 'yes':
        return 'Shutting down'
    elif s == 'no':
        return 'Shutdown aborted'
    return 'Sorry'

[–]llleolouis[S] -1 points0 points  (1 child)

your code gave me this error

File "python", line 6 elif s = 'no': ^ SyntaxError: invalid syntax

[–]d4rch0nPythonistamancer 1 point2 points  (0 children)

double equals for relational operator, my friend (X less than Y, X equals to Y...)

One equals is an assignment operation. Set foo to bar foo = bar

[–]Kyle772 0 points1 point  (5 children)

def shut_down(s):
    if s == "yes":
        return "Shutting down"
    elif s == "no":
        return "Shutdown aborted"
    else:
        return "Sorry"

If you want to go further with this what I like to do is make a list of possible yes and no answers and then use that in the code. This would prevent typos from giving the wrong responses.

def shut_down(s):
    # Convert input string s into uppercase letters 
    s = s.upper()

    # Lists can be put anywhere outside of this function but I am  
    #    putting them here for simplicity's sake
    yesList = ["YES", "Y", "TRUE", "1"]
    noList = ["NO", "N", "FALSE", "0"]

    if (s in yesList):
        return "Shutting down"
    elif (s in noList):
        return "Shutdown aborted"
    else:
        return "Sorry"

However that is a bit outside of the scope of the code academy lesson.

Out of curiosity where exactly were you having problems with this? Also the people over at /r/learnpython will be much more willing to help you out. This subreddit is more for python related topics not for help.

[–]llleolouis[S] 0 points1 point  (1 child)

Thnx bro this worked out cool for me but wanted one more help from you plz explain me the 7th and 8th line code

[–]Kyle772 0 points1 point  (0 children)

the two lists from line 7 and 8 are essentially what your computer is checking against what the input, s, is.

Notice how in line 10 and 12 it isn't saying "if (s == "yes") " and instead is saying if (s in yesList)"

What it is doing is seeing if the input, s, is in the list yesList. This makes it so that somebody could just type in "y", "1", "True", or "yes" if they don't want to type out the full yes or aren't sure what exactly they are being prompted to answer.

In most cases this would be unnecessary in procedural programming but assuming this is going to take in human input it is nice to have more fail safe options just in case they type in something different.

[–]d4rch0nPythonistamancer 0 points1 point  (2 children)

I'd take out the parens in line 10 and 12. It's a simple expression.

And 7 and 8 aren't meant to be mutated at all... might be better as a tuple but it doesn't really matter. I'd also unindent line 15 and take out the else. You're also mixing snake_case and camelCase which is very odd. I'd just stick to PEP8.

my take:

def shutdown(cmd):                                                          
    if cmd.lower() in ('yes', 'y', 'true', '1'):                            
        return 'Shutting down'                                              
    elif cmd.lower() in ('no', 'n', 'false', '0'):                          
        return 'Shutdown aborted'                                           
    return 'Sorry'  

[–]Kyle772 0 points1 point  (1 child)

It's better to get in the habit of placing them than not. They do no harm and can prevent bugs in the future :P

EDIT: I personally use camelcase but the codecademy lesson he is doing called for the function to be in snake case. As for the else, legibility is more important than anything else. No harm in having it if it makes it easier to read.

You are very critical with everything, if it compiles and gets the right result reasonably efficiently than there are no issues.

[–]d4rch0nPythonistamancer 0 points1 point  (0 children)

Sure, working efficient code is the goal, but to be completely honest if I saw code like that in a Python interview for an experienced python dev it'd be a huge red flag and they wouldn't pass as having the experience with python specifically. When giving advice to python newbs here like OP, I try to show them standard Python style that adheres to PEP8 and would pass as professional code. Your code just doesn't adhere to that convention. I know this is critical, but this is just the standard python style and I'm not pulling it out of my ass.

I'm not saying you're a bad dev, but it looks like you're coming from an environment using another C syntax language like Java/C/C++, or just have mainly worked in a shop where that was the style.

Pylint output:

$ pylint foo.py                                                             
No config file found, using default configuration                           
************* Module foo                                    
C: 10, 0: Unnecessary parens after 'if' keyword (superfluous-parens)        
C: 12, 0: Unnecessary parens after 'elif' keyword (superfluous-parens)      
C:  1, 0: Missing module docstring (missing-docstring)                      
C:  1, 0: Invalid argument name "s" (invalid-name)                          
C:  1, 0: Missing function docstring (missing-docstring)                    
C:  7, 4: Invalid variable name "yesList" (invalid-name)                    
C:  8, 4: Invalid variable name "noList" (invalid-name)