Strings aren’t reserved to words, they’re just a sequence of characters (as of python3-unicode characters and we’ll ignore binary encodings for now). I’m assuming they were converted to strings so as to make it easy to print and combine them other strings of text you added. For the example the following code would throw an error-
test = "this_string" + 3
Much easier and more readable just to convert it to a string "string" + str(3) rather than play around with binary.
There’s an error in the print statement itself as you’re attempting use a name input_number that isn’t defined.
Remember that the names inside the function are not accessible outside the function, e.g.-
x = 3
print(x) # inevitably throws an error. x does not exist in the main scope of your code
You could create a new name and use your print statement and function call in that fashion, e.g.-
intput_number = 16
# perhaps a different name would be appropriate to avoid confusion with the function name
result = divide_by_four(input_number)
print (input_number + ' divided by 4 is ' + str(result))
You can only return once from the function so what you would need is to return one or more values at once. This is generally achieved with a different data type (e.g. a sequence like a list or tuple). You could for example return your two useful values in a list.
def calc(<example>): # skipped these bits for this example
left = <example>
months_remaining = left / 4.0 # If you need integers consider floor division (//)
return [left, months_remaining]
retainers_left, months_left = calc(1, 1)
For an example like this one it would be typical to return a tuple rather than a list but they may not be introduced for a while (I may be wrong) so a list will do. The basic idea stands that you return a datatype that can store more than one value.
def calculate_age(current_year, birth_year):
age = current_year - birth_year
print("I am "+str(my_age)+" years old and my dad is "+str(dads_age)+" years old.")
Someone has asked this question above, although, I’m still unclear on the reason for the “return age” line. I assume at the time (without looking ahead to the next steps in the exercise), that I would reference age at some point.
Per first paragraph in the “Returns” chapter:
Functions can also return a value to the user so that this value can be modified or used later.
Not once did I actually build on the value stored in “return age”. Instead, I used the function to calculate new ages. I’m not sure this exercise actually taught me how to use return.
The example within the chapter, uses return and then references it again for a new calculation:
Here’s an example of a function divide_by_four that takes an integer argument, divides it by four, and return s the result:
You calculate a value and assign it to age within your function. Your return is passing that value back to the caller. If you didn’t pass a value back then your couldn’t assign your ‘new’ ages, namely the variables my_age and dads_age.
There’s not a lot of difference between this and your example divide_by_four, it could for example be rewritten as-
This operates in the same manner but assigns a name for the calculation new_number locally within the function. For these examples you could skip this assignment but for more complex functions you’d want to keep your code readable likely with multiple local assignments in the function.
Why does the following code not work in the “Write a Function exercise”? I realize this is not part of the lesson. I was trying to write a function that called for user input, and found that did not work. I then simplified the code and found that even the following did not work.
# Taking input from the user
string = input()
It might help if you specify the error you’re getting but a lot of CC lessons aren’t set-up to take user input. You’ll get a rather unclear error (end of file I believe) for using input in this case. Try it on your own system if you just went to practice using input.
Why not test it cleanly? Save the working code for that lesson elsewhere, clear it all and include a single line that uses input, no print statements or otherwise. Just the one line using e.g. test = input() and I’m pretty sure you’ll still get that error. Replace the working code when you’re done.
The exercise instructions are as follows:
The function calculate_age in script.py creates a variable called age that is the difference between the current year, and a birth year, both of which are inputs of the function. Add a line to return age .
what is the point of writing “age = current_year - birth_year” in the function if I cannot print(age)?