I have a basic buzz program written in python with return function. If i change return with print,it works fine but i want to know whats wrong with return statement.Can anyone help me whats wrong with this
#!/usr/bin/python
def div4and6(s,e):
for i in range(s,e+1):
if i%4 == 0 and i%6 == 0:
return "Hello"
elif i%4 == 0:
return "Hell"
elif i%6 == 0:
return "Hel"
else:
return i
a=input("Please input the start and end no: ")
b=input("Please input the start and end no: ")
print div4and6(a,b)
You have indentation errors in your code. Indentation is very important for python.
If python sees a return statement, it will simply goes out of the function back to the caller; regardless of the loop it is in. So your logic would be wrong if it's a return instead of print.
If this is a classwork/homework question, there's a different section in this forum.
Is this a homework item? Until you answer this question, we are unable to provide much more help.
You know indentation is important in python scripts. But the script you posted has indentation that is different from the code you're using. How do you expect us to help debug your code if you don't show us an accurate representation (in CODE tags) of the code you're using?
As balajesuri said, the first time through the loop in your function (with i set to 1) ends with the statement:
return i
and the return statement terminates the function and returns to the caller. If you change all of the return x statements in your function to print x statements and change the call to your function from: