Right, so I'm doing a homework assignment and I am asked to do the following:
Create a function called student data, that takes four parameters, a name (a string), age (an integer), student number (a string) and whether they are enrolled in CSCA08 (a boolean), and returns a string containing that information in the following format: [student number,name,age,enrolled].
Your code should work as follows:
>>> student_data("Brian",32,"1234567",False)
`[1234567,Brian,32,False]'
>>> student_data("Nick",97,"0000001",True)
`[0000001,Nick,97,True]'
What I came up with was this:
def student_data(name, age, student_number):
return '[' + student_number + ',' + name + ',' + str(age) + ']'
and when entering it into Python:
student_data("Jeremy", 19, "999923329")
'[999923329,Jeremy,19]'
(Note I left out the last bit about booleans - I'll get to that in a second.)
From what I understand, 'Jeremy' and '999923329' are strings which were both subsequently returned as part of the string in the second line. For 'age' since there were no quotations when I called the function student_data, it was interpreted as an int by Python. Then, I converted that int value into a string, so I could end up with '[999923329,Jeremy,19]'.
So technically, I guess what I'm asking is: is the parameter 'age' considered an int by python, until the return function changes it into an str type? Note that the assignment wants four parameters, two strings (which I have), one int (which I don't know if it's actually interpreted as an int) and a boolean, which leads to the following:
I really have no idea how Booleans work. Specifically, in the context of the assignment, what exactly am I supposed to do? What would an example be? I fiddled around with my code for a bit, and I came up with this:
def student_data(name, age, student_number, boolean):
return '[' + student_number + ',' + name + ',' + str(age) + "," + str(boolean) + ']'
And entering it in Python:
student_data("Jeremy", 19, "999923329", True)
'[999923329,Jeremy,19,True]'
That actually follows exactly what the assignment wanted me to do, but I don't like it because I don't really understand what's going on. Like, 'boolean' is a parameter that the function student_data needs to work. But what is a parameter exactly? Is it the same thing as a variable? When I enter 'True' in the python shell, what exactly is happening?
Is what is happening the same thing that happens when you assign a value to a variable? In that case, what is happening when I assign a value to a variable? The assignment is asking for a parameter to be a boolean, but I don't believe I entered a boolean into the code, did I?
And yes, if it isn't obvious already, I've never had a computer science class before.