17. Review: Functions response "NONE"


#1



https://www.codecademy.com/courses/python-beginner-c7VZg/4/1


My code is correct and I don't get any error message however, it's not prompting me or allowing me to enter anything so how do I know that it functions as instructed. All I get as a response is NONE


Prompt for a 'yes' or 'no' and print out an appropriate response.


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


#2

Add print shut_down("yes") to your code if you want to run it.


#3

I did that however it still doesn't give me the option of entering a response. Now it gives the response "Shutting down" None


#4

Sorry about that, I misread what you were trying to do. You should be able to use input() with the function to prompt the user. For example,
print shut_down(input("Please enter yes or no: "))

Now, because codecademy uses Python 2.7 and some stuff on top, that may or may not work. If it throws errors like 'yes' is not defined, then use raw_input() instead of input()


#5

Thanks that fixed the issue however, when I was checking to make sure that I get "Nope" when I enter anything other than a number (lets use the letter "a" as an example) I get error messages instead of "Nope". Also I noticed that if I use raw_input it returns everything as Nope because it thinks everything is of the type Unicode. However, if I use input then it works fine until I try to check non integers and non floats. Is there something that allows for both? Sorry to keep bothering you but you've been most helpful.


#6

It's not a bother at all, I enjoy this type of thing. What are you using to check if it's not a number?


#7

I used the letter "a".


#8

Right, but what is the code that you are using to check if it's a number?


#9

def distance_from_zero(n):
if type(n) == int or type(n) == float:
return abs(n)
else:
return "nope"
print distance_from_zero(input("enter a number:"))


#10

Unfortunately there is not an easy way to do this. If you use raw_input(), like you said, everything becomes type unicode. Input(), however takes everything as straight python code, this works fine if all we are inputting is numbers, but if we enter letters or words, we have issues. You can enter your letters or words with quotes so python recognizes them as strings, but that is not practical.

The best that I could come with uses a second function to check if the string can be converted to float. If it can, that function will return True, if not False. We can use that output in our main function and simply convert the unicode to float if the first function returns True.

def is_float(s):
    try:
        float(s)
        return True
    except ValueError:
        return False

def distance_from_zero(n):
    if is_float(n):
        return abs(float(n))
    else:
        return "nope"

print distance_from_zero(raw_input("enter a number:"))

#11

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.