Functions and PygLatin!


#1

Hi! I was just experimenting with functions and PygLatin:
Could anyone tell me what's wrong with my code?
I wanted to see if I entered an integer would it return "Thank You"
Why isn't it working?

print "Welcome to Python"
original = raw_input("Enter an Integer")
def integer(n):
    if type(n)==int:
        return "Thank You"
    else:
        return "Try Again"

Welcome to Python
Enter an Integer 2
None


#2

It will if we call the function on original:

integer(original)    # Thank You

There are other tests, though, as digging through the forums will reveal. Which is most appropriate will be up for debate.

A name for the function might be, is_integer, and have it return True or False. From a readability stand point, it is more semantic.


#3

If you need to use the Variable "original" in the case of n, then simply change all the "n" to original OR the Variable original to be called " n = raw_input("Enter an Integer")

this way when you ask it " if type(n) == int: " it interprets it as " if type(original) == integer: return "Thank You" etc..


#4

Got it! Thank you. :slightly_smiling:


#5

this is the code I entered:
print "Welcome to Python"
n = raw_input("Enter an Integer:")
def original(n):
if type(n)==int:
return "Thank You"
else:
return "Sorry"
print original(n)

Welcome to Python
Enter an Integer: 2
Sorry
None

I thought that if I entered the integer in the interpreter that would be enough for it to return "Thank You"
but "Thank you" is being returned only when I enter the integer in the editor.
I thought it'd work like it did in Python. You enter "PYTHON" and it becomes "YTHONAY" on its own.

print "Welcome to Python"
n = raw_input("Enter an Integer:")
def original(n):
if type(n)==int:
return "Thank You"
else:
return "Sorry"
print original(2)

Welcome to Python
Enter an Integer:
Thank You
None