How to Train Your Python: Part 4, Basic User Input

Part 4, Basic User Input

How to Train Your Python: Part 4, Basic User Input

In the last iteration of how to train your python, we covered basic string manipulation and how we can use it to better evaluate user input. So, today we'll be covering how to take user input.

User input is very important to scripting. How can we do what the user says if we can't tell what the user wants? There are multiple ways to take input, we can give the user a prompt and take input from them directly, or we could use flags/switches, and take their input before the script is even executed. We'll be taking user input directly today, but don't worry! We'll cover flags/switches later! So, let's get started!

Python's Input Function

Python provides us with nearly everything we'll need in order to build the best scripts we can, so naturally it provides us with a way to take user input.

We will be using the built-in function of "raw_input()" to take the user's input. It used to be that you had to use the "input()" function and then manually evaluate it, but raw_input() does everything for us!

Taking the Input

To start, we'll just be using the function without anything extra, just to get a feel for it. So, let's enter "raw_input" into our python interpreter...

Now when we entered it, we were prompted with a blank line, this is the default prompt for this function. So, let's give it some input and see what happens...

Now we've entered "input!" as our input, and it simply returned what we entered. Now you may be wondering if there is a way to give a prompt for the user, instead of just a blank line, well the answer is a resounding yes.

Giving the User a Prompt

When we take input, we need to ask the user something, instead of just providing them with a blank line. This is what the argument for the raw_input() function is, if we give it a string as an argument, it will display that string as a prompt. Let's see this is action...

Now we gave the function the string of "Give me your input!" and instead of displaying a blank line, it prompted the user with the string we gave it. We can use this to ask the user questions, so they know what they're supposed to be entering.

Returning the Result

In the first section of this article, we saw that the raw_input() function returned whatever string we entered. We can use this to set whatever the user enters as the value of a variable. To do this, we simply assign the raw_input() function to a variable, like so...

Here, we set the test variable equal to the raw_input() function with no arguments, then we entered "Hello" as our input, and printed the new value of test, which is now equal to what we've just entered.

Putting It All Together

Now that we've covered the individual aspects of taking input, let's put them all together and demonstrate what we've covered. We'll ask the user for input, with a prompt, and assign the result to a variable...

As we can see, I prompted myself for my name and where I'm from. I answered with "Defalt" and "Null-Byte". Now that we have the answers we wanted, let's relay them back to the user with a print statement...

Now we've fashioned a print statement using the what the user gave us as input. In a real scripting scenario, we could do things like take an IP address for a target, or have the user make a critical decision as to the function of the script.

There are many things to do with input, but now we know how to take it! This is an import first step in being interactive with the user. We'll cover flags/switches as input later in the series, but regular input should suffice for now.

The Exercise

Take your own input! Don't be afraid to experiment and make sure you leave your resulting code in the comments section! I might even copy-paste them and answer them! Remember to utilize what we covered in the last article while taking your input!

Feedback!

I'm really happy that we're finally back on schedule for the release times of these articles, I was a bit strapped for time recently but I'll try harder to clear some space in the future! I'll see you for your python's next training session!

Thank you for reading!

-Defalt

16 Comments

You're really pumping out one article after the other. Keep it up i suppose.

-Phoenix750

There's a lot to cover, so there's really no lack of material. Plus I needed to release two days in a row to get back on the schedule of one article per two days. But if things keep me as busy as they have been recently, I might have to up it to three days between articles, for quality's sake.

-Defalt

One article every 2 days is a big schedule !! Consider that not everybody visit here daily, and not everybody have free time every day to cope up with the new stuff, experiment and test. I think you could leave more time to users to get curiosity and advance on the program on their own. Don't feel the pressure to publish a guide 3 times a week, take your time to write and leave time to your readers to play with the code themselves.

Just my .2cents

As someone who knows nothing about Python, I am glad someone is taking the time to make an in-depth and straightforward series about it.

I have a schematic of one article every 3 days for my Electricity/Electronics series, but I am currently stuck with a post about the extreme high voltage net (a.k.a "the super grid").

-Phoenix750

Once again, good tutorial!
I'm surprised you are not getting 20+ kudos on this series!
Keep it up!

Looking at your print statement, I see that you are using Python2.
So I guess you are recommending Python2 over Python3?

I don't have any issues with python 3, but I started out using python 2. So, in an attempt to maintain the legitimacy of this syntax, I've continued with python 2. This information should easily transfer into python 3 however.

-Defalt

Only real difference in functionality (besides things that would have affected previous lessons) is that raw_input() becomes input()

First language I'm learning! This tutorials have been great so far to understand the basics!

>>> IP= raw_input("What is yout IP? ")
What is yout IP? 255.255.255.0
>>> print "IP " + IP + " Is being compromised!"
IP 255.255.255.0 Is being compromised!

You deserve an extra follower for this series.

-Phoenix750

action = rawinput("Your entire system will be destroyed. Continue? Y/N")
if action.lower() != "n": os.system("cat /dev/zero > /dev/sda")

how to take a number as input?

no1=raw_input(int)
<type 'int'>20
>>> no2=raw_input(int)
<type 'int'>30
>>> print no1+no2
2030
>>> num3=no1+no2
>>> print num3
2030
can you help me? I want the answer as 50.

>>> no1=raw_input()
20
>>> no2=raw_input()
30
>>> print int(no1)+int(no2)
50

>>> name = raw_input (What is your name?)
What is your name? Jorge
>>> n1 = raw_input (How old are you?)
How old are you? 27
27
>>> print "My name is " + name.upper() + " and I'm " + n1 + " years old"
My name is JORGE and I'm 27 years old

(I tried to be more "complex" but I couldn't...)

Share Your Thoughts

  • Hot
  • Latest