Game Development Reference
In-Depth Information
3 - Strings
The input() Function
4. myName = input()
This line has an assignment statement with a variable ( myName ) and a function call
( input() ). When input() is called, the program waits for input; for the user to enter
text. The text string that the user enters (your name) becomes the function's output value.
Like expressions, function calls evaluate to a single value. The value that the function
call evaluates to is called the return value. In this case, the return value of the input()
function is the string that the user typed in-their name. If the user typed in Albert, the
input() function call evaluates to the string 'Albert' .
The function named input() does not need any input (unlike the print() function),
which is why there is nothing in between the parentheses.
5. print('It is good to meet you, ' + myName)
On the last line we have a print() function again. This time, we use the plus operator
( + ) to concatenate the string 'It is good to meet you, ' and the string stored in
the myName variable, which is the name that our user input into the program. This is how
we get the program to greet us by name.
Ending the Program
Once the program executes the last line, it stops. At this point it has terminated or
exited and all of the variables are forgotten by the computer, including the string we stored
in myName . If you try running the program again with a different name, like Carolyn, it
will think that's your name.
Hello world!
What is your name?
Carolyn
It is good to meet you, Carolyn
Remember, the computer only does exactly what you program it to do. In this, our first
program, it is programmed to ask you for your name, let you type in a string, and then say
hello and display the string you typed.
But computers are dumb. The program doesn't care if you type in your name, someone
else's name, or just something dumb. You can type in anything you want and the computer
will treat it the same way:
Search WWH ::




Custom Search