Reputation: 19
just casually learning python over here and trying to write fizzbuzz. I'm reading a book to get me going but just want to try out stuff myself because I think that's going to work out the best for me as it generally does when learning stuff. So this is what I've got:
def input_and_test():
print "Gimme something and it better be a number or shit ain't gon work"
inputt = raw_input()
inputt = str(inputt)
try:
input_data_type = int(inputt)
except ValueError:
print "Yeah that sure as hell ain't a number, sonny"
input_and_test()
else:
print 'Great, now lemme check'
input_and_test()
input_lenght = len(inputt)
input_lenght = int(int(input_lenght) - 1)
last_number = inputt[input_lenght]
if input == 1:
print 'Even'
elif last_number == 0 or last_number == 2 or last_number == 4 or last_number == 6 or last_number == 8:
print 'Even'
else:
print 'Odd'
Now I'm sure this is horribly inefficient but that doesn't matter because I want to learn how to do it this 'way'. It throws this error though:
Traceback (most recent call last):
File "C:\Python27\test.py", line 16, in <module>
input_lenght = len(inputt)
NameError: name 'inputt' is not defined
Why? Did I not define inputt in the first function that I just called? Is there a better way to replace the exception handling and start the function again part that fixes all of this? Any bad things about this code (apart from it not being optimal fizzbuzz) that I should avoid in the future? Again sorry for this being hilariously bad and this question being hilariously simple probably. I appreciate any and all help!
Upvotes: 0
Views: 59
Reputation: 19382
You have to return the value from your function.
At the end of input_and_test
add:
return inputt
Then, in every place where you call input_and_test
, store the value:
inputt = input_and_test()
You have to do that because inputt
is local variable inside input_and_test
, so each time you call that function, a new inputt
variable is created inside it. Once the function exits, the variable is gone.
Upvotes: 1
Reputation: 36708
As Barmar said in a comment, you should really use return
to get your function's output out into the calling code. Here's how you could have written your function better with return
:
def input_and_test():
print "Gimme something and it better be a number or shit ain't gon work"
inputt = raw_input()
inputt = str(inputt)
try:
input_data_type = int(inputt)
except ValueError:
print "Yeah that sure as hell ain't a number, sonny"
return input_and_test()
else:
print 'Great, now lemme check'
return inputt
inputt = input_and_test()
input_lenght = len(inputt)
input_lenght = int(int(input_lenght) - 1)
last_number = inputt[input_lenght]
if input == 1:
print 'Even'
elif last_number == 0 or last_number == 2 or last_number == 4 or last_number == 6 or last_number == 8:
print 'Even'
else:
print 'Odd'
In the code above, I've left your variable names alone, with the name inputt
being used in two places. But that might confuse you, because the inputt
inside the function is not the same variable as the inputt
outside the function. So I'll rename your inside-the-function variable to a different name (personally, I like using the name result
for "the value that this function is eventually going to return), so that it will be easier for you to see what's going on:
def input_and_test():
print "Gimme something and it better be a number or shit ain't gon work"
result = raw_input()
result = str(result)
try:
input_data_type = int(result)
except ValueError:
print "Yeah that sure as hell ain't a number, sonny"
return input_and_test()
else:
print 'Great, now lemme check'
return result
inputt = input_and_test()
input_lenght = len(inputt)
input_lenght = int(int(input_lenght) - 1)
last_number = inputt[input_lenght]
if input == 1:
print 'Even'
elif last_number == 0 or last_number == 2 or last_number == 4 or last_number == 6 or last_number == 8:
print 'Even'
else:
print 'Odd'
There, now the two different variables have two different names.
Now, do you see how I have two different return
statements in the function? Is it obvious to you why, or do you need me to expand on this answer to explain why the return input_and_test()
line is necessary?
Upvotes: 3