Full Transcript

Input and Variables Input and Variables User Input Let’s make our programs a little better. Using code name.py in the terminal, we’ll add another function. input() input is a function that will return some value. In this case, it will pause the program and wait for the user’s input before r...

Input and Variables Input and Variables User Input Let’s make our programs a little better. Using code name.py in the terminal, we’ll add another function. input() input is a function that will return some value. In this case, it will pause the program and wait for the user’s input before returning it to the program. Functions that return some values are sometimes called fruitful functions. input("What's your name?") print("Hello World!") name.py Predict Pause the lesson and predict what this code will do. Run it after your prediction and come back to this lesson. Did it do what you expected? Probably not. While input did return the user input to the program, without a way to store that value, it goes into the ether. We need a mechanism to store that value, so we can refer to it later in our program. For this to work, we will need to introduce you to variables. Variables Variables are simply storage containers to hold data/values in a program. Computer memory cells if you will. Modify In your program, let’s make the below modifications: name = input("What's your name?") print("Hello") print(name) print("!") name.py Note the = operator is an assignment operator. It values to variables. On the left side of the = is our variable. We have given it a name called name. We’ll talk more about how to name your variables later. On the right side of the = is our call to the input function that will provide the value of the user input to assign to our variable container. Predict Predict what this will do, then run it! Un-pause the video when you are done. Let’s run it together. What's your name?Frodo Hello Frodo ! So we are now able to print the value of name by simply passing it as an argument to the print function. We’re getting close. We’ve got the user input to display now. Let’s try to get them all in a line though. Parameters In order to do that, let’s talk about function parameters. Besides passing arguments to functions, we can specify parameters and assign values to them. This is can be helpful to add features to change the behaviour of functions. Let’s modify our code as follows: name = input("What's your name?") print("Hello", end="") print(name, end="") print ("!") name.py Predict Now, predict what this will do. Pause the lesson and write down your observations. Let’s run this together. The results show all characters together without spaces. So if you guessed right, the end parameter controls what is output at the end of a print command. By default it will output \n or a new line. Let’s see that in action here: name = input("What's your name?") print("Hello", end="") print(name, end="\n") print ("!") name.py Now this is the default behaviour, but I would like to show you the special \n new line character. Modify Pause the lesson and try modifying the value of the end parameters to add a space between “Hello” and the name and print the sentence in a single line. Let’s modify our code to achieve our desired results. name = input("What's your name?") print("Hello", end=" ") print(name, end="") print ("!") name.py I hope you got it right. Different functions have different parameters. So it’s always a good idea to check out the documentation to see what’s available. Summary We learnt to use the input function to gather user keyboard feedback. We learnt that functions can return some values and if we want to use it later we have to store it in a variable. Then we learnt to assign that value of the input to a variable using the = operator. We learnt that a variable is a storage container to store all sorts of data, that we can retrieve for use in our program. We learnt that we can specify different parameters to change the behaviour of our functions. And finally we learnt that \n is a special character that gives us a new line.

Use Quizgecko on...
Browser
Browser