[ERT ICT Teaching] Learning Python for JSS (14 - More on input statement and function nesting)

preview_player
Показать описание
A complete series of learning python and computational thinking.
Simple enough for junior secondary school students in Hong Kong.
Рекомендации по теме
Комментарии
Автор

Script:

Hi, I am Mr. Eric Tang. In video 10, we've learnt to use the input statement to get inputs from users and we know that Python will treat all inputs as strings. If we want to do mathematical calculations by the input data, we have to change the data to either integer or floating point number data type.

Just like this example here, I store the user input data to variable A and then immediately convert it to a floating point number, and output the result of adding the number by 1. Here is the

I input 4 and you see the answer is 5.0 as expected.

Going back to the codes here... For more advanced programmers, they will combine these two lines in this

Let me explain this like this mathematics expression, 3*(2 / (2+4)), we'll always do the things in the bracket first... here we will first do 2+4, which is 6, and then do 2 divided by 6, which is 0.33... and then finally do the 3 * 0.33... which is 1.

Therefore, here the computer will first do the input statement to get user suppose a user inputs 3.8, and then the input data will be put to the float function and then the data will be treated as a floating point number, and the floating point number will finally stored to a.

Here you can see the input of this float function is actually from the output of another input function. We call this "Nesting" of function. It's just like a bird nest, the the brackets are like each layer of the bird nest.

Another way of seeing nesting is that we can treat each function as a machine. Here as you can see, the machine called "INPUT" will get the input from the user, and the output of this INPUT machine will be fed into the other machine called FLOAT. the output of this FLOAT machine will then finally stored in the variable a.

Knowing how to nest functions together can help you to build a more complex statement. Lets go back to the codes.

Here we can further nest functions Now we can further convert the data from floating point number to integer....

Lets run the program

Now I input 4.99...

You see! the input is treated as 4 and the output of adding 1 is 5!

Thanks for watching! If you like this video, you can give me a like or subscribe my channel. Good bye!

ertxxx
welcome to shbcf.ru