which caused an error but then i got ride of the () in the return and it worked fine, my question is why? (in the post the indents are off for some reason so disregard the indentation issue)
I understand return in general and know it will return None, if nothing is present etc. but I don’t understand why no () vs. including () makes a difference?
While placing parentheses around the return value is unnecessary, it does not affect the result. Therefore, it should not cause a problem. What error message did you see when the parentheses were included?
@ion_killah, in general, it’s probably considered better form to omit the parentheses with return. Since return is not a function, they are not required, and the (definite, but not overwhelming) preference seems to be to omit them.
return can evaluate and return a sequence, called a tuple, of expressions.
If you have not yet studied the tuple data type, it is a data type consisting of a sequence of items separated by commas. By default, Python prints tuples surrounded by parentheses, leading people to suppose that parentheses are required, but they are not, unless it’s necessary to place them to avoid ambiguity (for instance, if the tuple contains mathematical expressions requiring parentheses.)
# cool things you can do with tuples
t = 5, 7 # tuple packing
print(t)
print(t[0], t[1]) # the print function can take a tuple as an argument
a, b = t # tuple unpacking
print(a)
print(b)
print("*******")
# Here is a function with multiple expressions in the return statement: a tuple
def sq_cube_fourth(x):
return x**2, x**3, x**4
q = sq_cube_fourth(5) # call the function
print(q) # this will be a tuple
r, s, t = q # now let's unpack it
print(r)
print(s)
print(t)
''' Output:
(5, 7) # Python adds parentheses
5 7
5
7
*******
(25, 125, 625) # again with the parentheses
25
125
625
There are other rules, the most important being that you can’t change the items within a tuple, as you can with lists. As with strings, if you need to change something, you must build a new tuple.
Including the parens casts the return value to a tuple, which might not be the expected return from the standpoint of the SCT (or the caller, for that matter). They do have an effect on the return value, make no mistake.
You’re welcome. It is a bit unexpected if you are coming from JavaScript. JS ignores parens except in grouping for precedence concerns. There is no such thing as a tuple in that language.
Surrounding a single expression with parentheses will not convert it to a tuple. To create a tuple from a single expression, we need to place a comma within the parentheses after the expression.
See the following:
class Spam:
def __init__(self, topping):
self.topping = topping
def get_topping(self):
return self.topping
# return(self.topping)
# return(self.topping,)
s = Spam("eggs")
print(s.get_topping())
Yeah, by focusing on a pattern in the user’s code rather than directly on the result, the SCT can mislead the user into thinking there was an error, when in fact there was not …
Messages from SCTs should strive to make it clearer whether they are a complaint from the SCT itself or whether they originate from the Python interpreter or even relate to the actual result, especially in the introductory courses. With experience, users can make the distinction, however, learners cannot be expected to recognize the difference right from the start.
Thanks, to all of you for clearing that up. Nice to know its just the backend system checking what I have done, which is sending that response out and not an actual error. For curiosities sake what does SCT?