Welcome to the Treehouse Community
Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.
Looking to learn something new?
Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.
Start your free trial
Artjom Dzug
8,238 PointsShould we place int ?
Maybe I got this wrong but when you defined the function spit_cheack, shouldn't you define that there will be a number? like def int split_check(.....):? In the video, you "total_due = float(input(....)" string without an integer but you have in the second string "number_of_people "you did put an integer, Why?
2 Answers
Alex Koumparos
Python Development Techdegree Student 36,888 PointsHi Artjom.
The short answer is no, it is not syntactically valid to insert a type between def and the function name.
Python is duck typed, so the interpreter will generally accept any type of input for a function or method and the person writing the function is expected to work with any type of input that the function might reasonably receive and gracefully handle the cases where the type received is not the type desired.
An obvious example of this is that a function that is handling some sort of numeric input might receive ints or floats and the function should be written in a way that it can handle either.
This is conceptually very different from languages that require explicit typing, and will only work if given the exactly correct type (e.g., Swift, Java).
Although the function signature is not explicitly typed, and so the arguments supplied when calling the function could be any type, once a function is actually called with real values, the variables are really that type. So if you give a function with a parameter 'x' an int, x is an int. If you call the function and pass in a float, x will be a float. As such it is necessary to explicitly cast from one type to another inside the function to work properly.
Here is a simple example to illustrate:
def make_bigger_by_two(input_integer):
return input_integer + 2
This function will take any type of input for input_integer (we gave the variable a name of input_integer, but we can't guarantee that we'll actually get an integer as an input). Suppose we can't control every situation where this function will be called, and we find that sometimes it might get called with a float. As written, the function will take a float without complaint, but will crash as soon as it tries to add different types.
We can work around this by doing something like the following:
def make_bigger_by_two(input_number):
return int(input_number) + 2
Note this will still crash if someone calls the function with a string, e.g., "three". And whether we decide to handle that situation depends on how likely we think it is that we'll get strings when we expect numbers. We might decide to wrap the type conversion with a try/except block to respond to input types that we can't sensibly add 2 to.
Hope that clears everything up
Cheers
Alex
Artjom Dzug
8,238 PointsThank you for the great answer !
Artjom Dzug
8,238 PointsArtjom Dzug
8,238 PointsThis question is for Python-> Python Basics-> Functions and Looping -> Returning Values