Show the Question
As well as numbers python can operate on text, called strings. Where a string is just a sequence of characters, in Python the type is named ‘str‘. Below is an example of using a variable to refer to a string. In the Python console type the following
question = 'What is your name? ' print(question)
The quotation mark lets Python know that a string is starting and a closing quotation to end the string. Where both single and double quotations can be used.
question = "What is your age? " print(age)
When talking about numbers we pictured that the number to be contained within a box. For a string we can picture a string as a stack of boxes where each box contains a single character.
Assigning a variable to a string can be pictured as attaching the label to the stack of boxes (not the boxes that contains the characters).
To refer to a single character in a string we use the index number of the box.The index reflects it’s position in the string, where the first character index is 0, second character is 1 and so on. In a Python console type the following ( or download from here)
name = 'Skelly' print(name) print(name) print(name) print(name) print(name)
Using IDLE to run the code should see the following
Rather than depicting these with images an easier way (to draw anyway) is with a diagram like below:
Note: In Python there is no type for a single character and each container in a string for a character is a string that contains a single value, in the Python Console type the following shows the class of the container used to store the first character of ‘Skelly’ is a string (str) itself.
name = 'Skelly' type(name) type(name)
As with numbers we can perform operations on strings, in a python console type the following:
'Spider' + 'man' 'Ho ' * 3
There is a lot more that can be done with strings in Python, and these will be explored later as needed, but first exploring how to use python to get an answer.
Get an Answer
After asking a question there needs to be a way to allow the user input an answer, using IDLE create a python file and type in the following code: (or download file from here)
print('What is your name? ') name = input() greeting = 'Hello ' + name print(greeting) print('"name" refers to a', type(name), end=' ') print('that contains', name)
When running we see that the input function waits for a line to be typed in, and returns the string of characters you typed. In the example above we used the variable ‘name’ to label the returned string containing the characters of the typed in line.
Note: instead of having to print the question we can place the question string in the input function, which reduces the amount of typing to ask a question and get an answer.
name = input('What is your name? ') print('Hello ' + name)
The string inside the parentheses, (), after input is called the prompt, prompting you that keyboard input is expected at that point. As the input function returns a string we need to add some more code to convert to an integer (int) or float when we expect the answer to be a number. Add the following code to the Python file created earlier ( or download full program/script from here)
age = input('What age are you? ') age = int(age) print('Your age is ', age) print('"age" refers to a', type(age), end=' ') print('that contains', age) # suspicious question next !!!, be careful # which questions you answer truthfully money = input('How much money is in your pocket you? ') money = float(money) print('You have €', money) print('"money" refers to a', type(money), end=' ') print('that contains', money)
Running in IDLE should see
Try running the program and type in answers that are not numbers to see what happens !!! (Generally for any program this is a good test as users of a program will always do something different to what the programmer expected. For example when asked age a valid answer is ‘nine’ rather than 9 as expected by the program above.