Hey guys, welcome to this lesson on
how the input() function works in Auditorium.
Now, this is an important lesson
because inputs actually work a little bit differently in
Auditorium compared to what you have seen so far elsewhere.
Now the first thing you'll notice is that in exercises
that require an input,
the Input pane will show up on the bottom left.
And here occasionally I might start you off
with some example inputs,
but if you see the Input pane, you can always delete
what I've written in there and replace it with your own,
or write multiple lines of inputs,
but how does it work exactly in Auditorium?
Well, it's mostly used for us to be able to test your code,
to be able to see that you are writing the correct code
and to make sure that you are on the right track.
So let's take a look at
how inputs work in a little bit more detail.
Take a look at the code that I've written here, print,
and then the input function,
and then closing the print statement.
Go ahead and run this code
and see what gets printed in the Output area.
Can you see where it comes from in the Input pane?
Now notice how I've added two more of these lines of code.
Now, if you go ahead
and run this code now as it is,
you should see all three names in the Input area printed
into the Output area.
So the way that input() functions work in Auditorium is
that you can go line by line from top
to bottom in the Input area,
and as long as you write the input function,
so the word, "input" plus the opening
and closing parentheses, it will look in the Input area
and pick out each one line by line.
But now we have an interesting problem,
what if I have four of these lines of code to try
and pull in four inputs?
While, if you take a look in the Input area,
there are clearly only three lines of inputs.
Jane, Ben, and Tom, there is no fourth input line.
So what happens here?
Well, if you go ahead and run this code,
you will see an error.
And this is where you are trying to pull in an input
that doesn't exist in the Input pane.
So if you familiarize yourself with these errors,
then if it does happen in the future,
you'll know exactly what's going on.
So when you do see that end of line, then you'll know that,
"Oh, maybe I've added one too many inputs,
and it doesn't actually exist in the Input pane."
Now the final thing I want
to mention is unlike previously when you've been writing
Python for your projects, when you use the input prompt,
because we don't need the prompt in order to get hold
of the input, this prompt actually goes into the output next
to the input name.
So when you run the code here, as you see right now,
you'll see the words, "What is your name?"
And then immediately the first line of the input.
Now this is not what we want
to do on the Auditorium coding exercises
because we're testing your outputs to make sure
that you're getting the right results from your code.
So in all cases, when you are using the input() in Auditorium,
I recommend to use it without any prompt text.
So simply the keyword input, an open parentheses
and a closing parentheses,
and that entire block will be replaced
by whichever Input you have in the input pane.
Now take a look at this Demo coding exercise.
Now without changing any of the code in main.py,
go ahead and simply click on the SUBMIT button
and run all of the tests that we've prepared for this exercise.
Now, one thing you'll notice is
that the code should pass all
of the tests in the Evaluation pane.
But notice in the Input area, you only see two numbers.
So the code here on Line 4 multiplies num1,
which comes from the first line of the Input area,
by num2, which comes from the second line
of the Input area.
Now, in order for us to be able to test your code
to make sure that you're doing the right things,
we will add hidden inputs and hidden tests.
So when you run the code,
you will see other tests being run in addition to the input
of 2*3.
This way we can ensure that your code is actually code
and it's doing what it's supposed to do,
and it covers all of the corner and edge cases.
Now notice what happens if a student completes the exercise
without actually completing the objectives of
what we're testing them on.
So in this case, we're pulling-in num1 from Line 1
of our Input, which is 2,
and we're pulling-in num2, which is 3,
and then we simply printed 6 on Line 4,
'cause I know very easily that 2*3 is 6.
Now, however, if you go ahead
and click on the SUBMIT button without, again, changing any
of the code in the Demo, then you'll see that one
of the test pass, but the other one fails.
And this is because
that the first test case is 2*3,
which does equal 6,
so your output from your code is 6,
and the expected output is also 6.
So that works just fine.
Now, however, in the second test case, this doesn't pass
because we're printing the output 6,
whereas we've got some hidden inputs testing your code
against the inputs of 5 and 5.
5*5 is 25, and 6 does not equal 25
and that's why the second test fails.
Now you might think to yourself, well, how can I write code
that works for all of these test cases?
Well, you would have to write actual code like
what you saw in the previous slide, which takes in any input
and doesn't care what number it is,
and simply multiplies the first number by the second number.
So head back over there if you're confused,
and see if you can fix this slide so
that all the test cases pass.
Now, try it yourself, try pulling in some
of the inputs from the Input area.
So look at the Input pane at the bottom left,
go ahead and change the values that are in there,
you can add as many lines of input as you want
and then try writing some code in main.py in order
to print the different lines of input into the Output area.
If you get stuck, look back at the previous slides to see
how you can pull in the input using the input function.
Now in the final coding exercise of this lesson, I want you
to figure out how to pull in the inputs
and calculate the number of letters in the names
that I put into the input.
Now just before we do that, I want to show you
how you can actually get hold of the number
of letters from a piece of string in Python.
So the function in Python is called len(), which stands
for length, and you can use len
and then a set of parentheses,
and then inside the parentheses add some text such as
what you see here on Line 1.
And when you print the result of that,
you will see the number of letters in
that text being calculated.
Go ahead and hit RUN CODE and see this for yourself.
So now you're finally ready
to tackle the final coding exercise of this lesson.
So take a look through the instructions, the hints,
and see what we're expecting you to get as an output.
So the task for today is
to pull in the text from the input area.
There will only be one line of inputs,
and this will be a name.
So you can see in the starting example,
I put my name in there,
and your job is to write some code in main.py in order
to print out the number of letters in the name
that's put in the input.
Now of course, remember what I mentioned
before, when we're testing your code, we're going
to pass your code with inputs
that I have hidden away.
So when you submit the code, when you think you are ready,
you'll see different inputs being passed through main.py
and your code has to work for all of them.
So make sure you're writing code
and you're not just simply calculating it using your head,
and that way you'll be able to pass all
of the tests in the Evaluation pane.
So give this a go
and if you get stuck, there's always the next slide
where I'll go through the code solution,
but also you can head back a few more slides previously
and see if you can figure out the solution yourself.
Now we're going to break down the solution into three
distinct parts,
and the first part is the input() function.
The input() function can bring in the value
that's in the Input pane.
So that's the first part we need to get hold of.
Now that we've gotten hold of the text on the first line
of the Input pane, then we need to put it
inside the len() function.
So as I showed you earlier,
the len() function can take any piece of text
and tell you how many characters
or how many letters are in that piece of text.
So in this case, we're simply inserting
what we got from the Input into the len() function in order
to perform this functionality
and let us know how many letters are in the input text.
Now, of course, when you saw it before you simply saw len,
and then in the parentheses we added in plain text,
but in this case, we're writing our code
to perform things automatically
and it has to work for all of the different inputs, not just
for the word Bertrand.
So in this case, we're taking the input
and putting it inside the len() function so
that it'll work even if we switch out the input
with any other name.
Now, the final part is to put all of
that into the print() statement.
So remember, inputs go into the part
between the parentheses.
So now we have a complete program
where we first take the input,
we put it into the len() function, we calculate the length
of the string in the input,
and then we put that into the print() statement
and we print it out into the Output area.
So each of these functions had a distinct role,
and by putting in them one into the other, we end up
with the result we want.
Now, if you got stuck
or if you had any differences in your code, feel free
to go back to the previous slide and update it
now. As long as you understand exactly what's going on here
because there's several steps involved, then I'm happy
that you have mastered the art of the input() function.