We’ve managed to create our first program to analyze the keyword search volume of five keywords and put them in a graph chart.
But what if you want to research some other keywords? There are two ways you can do this:
1: Go into your program, and replace the keywords in the “key_words” list manually each time.
2: Re-write your code that accepts user input and passes the information along to the program to process
In the long run, Option 2 is the most effective one.
In this blog post, you’ll learn:
- How to get the program to accept user input
- The 3 different types of input you need to know about
- How to pass user input into a variable
The good news is that we don’t need to write a new program – all we have to do is edit the program we wrote in Part 2.
If you haven’t done the exercise in Part 2, make sure you do that part first<link>.
Before we start writing the actual code, we need to look at user input – specifically, the three different kinds of input values.
Types of User input Values
There are 3 main kinds of user input:
- String: a piece of text, indicated with quotation marks e.g. “Salutation, planetary sphere!”
A string variable in Python looks like this
string = str("Salutation, planetary sphere!")
- Int: short for “integer” – a non-decimal number, e.g. 34
In Python, an integer variable looks like this
integer_value = int(34)
- Float: a decimal number, e.g. 34.1
In Python, a float variable looks like this:
float_variable = float(34.1)
How to get the user to input data
The first thing we need to do is create that will accept user input for the five keywords you want to research. This is done with the following command:
keyword1=str(input("Enter the first keyword:"))
–“keyword1” is the name of the variable
– “str” tells Python to convert the input value as a string (i.e. a text value )
– “input()” tells Python to prompt the user to provide input
– “Enter the first keyword” is the text that will be displayed to the user
Next, let’s create the same command for the remaining four keywords we want to look up. It could look something like this:
Once that’s done, replace all the keywords in this list:
With this (remember to replace them with the variable names you created earlier):
There you go – your program is now ready to accept user input! Well done!
Would you like to get rid of the confusing fluff and jargon in favor of something that will make your readers go, “Oh, I get it now!” after they’ve read it?
Then, contact me today, and let’s talk some more.