Whitespace in Python
Right away, when you start working with Python, you need to understand that Python uses whitespace to create blocks of code, or to delimit what we might call a code suite, in Python. You understand, at this point, we know nothing about many of these statements, but you can see, when using a class statement, the statements that are part of that class will be indented. Likewise, when defining a function, the statements that form that function will be indented. When you do if and elif and else type statements, the blocks of code that execute either when the if is True or when the if is False, those will be indented. Using for loops or while loops also has the use of indented statements. Now before each of these indents, you’ll notice that there’s a colon on the line above and then the statements themselves are indented four spaces.
User Input
If you want to allow your user some control over your program, then you can receive input from them. If you want to do that, you could use the input function in Python, which is built in. To find out more about functions like input that are built in, you can type help, followed by a pair of parenthesis containing the name of the function you want to know about. So in this case, help, parenthesis, input, parenthesis, displays the help on that built-in function, which we can see. There is, inside of the input usage summary, a square bracketed prompt value, which indicates that that is a optional parameter. We also see that this function will read a string from standard input, and strip the trailing new line. And it talks about ways to do this in Windows, or in a Unix-like platform. So let’s take a look at how we might use it.
help(input)
The output is displayed: Help on built-in function input in module builtins: input(…) input([prompt]} -> string Read a string from standard input. The trailing newline is stripped. If the user hits EOF(Unix: Ctrl-D, Windows: Ctrl-Z+Return), raise EOFError. On Unix, GNU readline is used if enabled. The prompt string, if given, is printed without a trailing newline before reading.
If we wanted to capture the user’s name, by prompting them, we could use a name variable to store the string that they type. Use that input function, with a prompt being a string: quote, What is your name, question mark – and it’s a good idea to leave a space, so that the prompt won’t be right next to the user’s cursor. I’ve put a space before that closing quote, and then have the closing parenthesis. Once this executes, you’ll see that it prints the prompt to the screen, and then waits for the user’s input. So the user might type their name, like Keith, and press Enter. Now you could inspect, by typing name in the interpreter, what the value is that’s held in that object. And we see the name string, which is Keith.
name = input('What is your name? ') On Enter press, a prompt is displayed: What is your name? The presenter enters "Keith" and presses Enter.
Now if you wanted to output the value of that name string to your user, you can’t just simply place the name in the file. You have to actually use a print function. So we’ll use print, to print something like a greeting; ‘Hello’. And then a comma to separate that value from the name. Type our closing parenthesis, and execute that by pressing Enter, and we can see that greeting appear on the screen. Now we just saw Hello, and name being printed out. If we wanted to print out additional information, like welcome to Python, we could do that by using additional values, or by doing string concatenation.
print('Hello', name) Output: Hello Keith
Notice, if you went to go print: quote, Hello, quote, name, it should say comma, name, comma, and then quote, comma, welcome to Python, exclamation point, quote – there’s kind of an awkward placement of the comma, because each of the values that’s printed is going to have a space automatically added after it. If we wanted to prevent that from occurring, we could combine the two strings together into one value to print.
print('Hello ', name,', welcome to Python!') The output is: Hello Keith, welcome to Python! print('Hello ', name +', welcome to Python!') The output is: Hello Keith, welcome to Python!
Now it looks like we have a complete program to receive input, and actually write some output. Before we can run it, we need to save it. So let’s do that with File – Menu – Save As. Already created a myscripts folder. I’ll go double-click it to navigate to that, and we might call this “hello_input.py”. Save it with that file name, and let’s test it by using Run menu – Run Module. We see the shell has restarted, and is prompting us for our name. So now we could type that in; Wright is what I’m going to use this time. And now the program responds with the output Hello Wright, welcome to Python! Now you can see a simple method for receiving input from your user.