Have you ever wanted your python program to ask❓ you for a password and then start running 🏃♀️as the other apps do?
Today, you will see, how to make python ask you questions.
That is, how can you take user input⬅ in python?
input() function’s official documentation.
Hello, Pythonistas🙋♀️ welcome back. In this article, we are going to see👀 how can you make your python program ask questions to users.
Contents
Previous Post’s Challenge’s Solution
But, before we get started let’s first look at the solution to the previous post’s challenge.
input_string = input()
input_string = input_string.upper() #to convert all letters of
#string to upper case
print(len(input_string)) #to count the total no. of characters
str_to_list = input_string.split(",") #to convert the string to list
#so that we can join the list with full stops
print(".".join(str_to_list)) #to join the elements of the list to
#form a full stop separated string
- We first took the string as input using the input function.
- Then, we converted all its letters to the capital case using the upper function.
- Next, using the len function we printed the length of the string.
- After that, we converted the string to a list using the split function so that we can replace commas with full stops.
- Finally, we joined the elements of the list to form a string separated with full stops using the join function and printed it using the print function.
If you were able to solve this challenge then you are becoming a very good python developer from the start because this is the first program that has the flow of user input->process->output.
Even if you were unable😞 to solve the problem it’s alright😊 you will slowly and gradually be able to build your logic just relax😌 and try to solve it at least☝ once on your own.
Now, let’s dive in deeper to see how can we make python ask questions. Like in the solution above.
How to make python ask questions?
You can make python ask questions and take input using the input function. Super easy right?
Example:-
name = input()
print(name)
Output:-
your name
your name
**The default datatype of whatever you input using the input function will be a string.
But, how will you get to know that python is asking you your name if you haven’t written this code?
The user of the program won’t read the program, he/she will just use it like you use your applications and games.
To solve this you can enter a prompt string.
Like in the following example:-
name = input("What is your name? ")
print(name)
Output:-
What is your name? your name
your name
The prompt string is nothing but a string you put inside the input function that is to be displayed to the user to let him/her know what is the program asking for.
How can we take multiple user inputs?
We can do this by using the split function.
You would have already done this if you would have solved the previous post’s challenge.
Let’s see this with an example.
names= input("What's your name?, What's your friend's name? ")
name1, name2 = names.split(",")
print(name1, name2)
Output:-
What's your name?, What's your friend's name? Me, My Friend
Me My Friend
Here, the split function separates the elements of the string by commas(Me
is one element and My Friend
is another). And in the same line, we store both these elements into different variables using multiple assignment.
You can take any number of inputs using this. There is another method for multiple inputs but for that, you will need to know about loops so that method is not explained here.
How can we take user input in other data types?
The default data type of whatever you input is always a string. But what if you are making a calculator you can’t use strings for them. You will need to use integers or floats.
For this, you can use functions like int, float, list, etc. See the example to take an integer as an input.
n = int(input("Enter a number:- "))
print(n)
print(type(n))
Output:-
Enter a number:- 10
10
<class 'int'>
You can also write it this way:-
n = input("Enter a number:- ")
n = int(n)
print(n)
print(type(n))
Output:-
Enter a number:- 10
10
<class 'int'>
Now let’s see what is argv and how is it different from the user input we saw.
What is argv and how is it different from user input?
The argv
is an argument variable which means it is a variable that works as an argument for our python file/script.
If you are using argv
in your program then you would need to run it in Command Prompt it will not work in IDLE or VS Code. (Don’t worry I will show you how👍)
For using argv
we will first need to import it from an inbuilt(comes with python installation) library sys
. To import argv
of sys
in your program simply type:
from sys import argv
This means that we are getting argv
from the sys
library into our python file. Now that you got an idea of what is argv
let’s now see a few examples to make it more clear.
from sys import argv
script = argv
print("The script is called:", script)
To run it:-
- First, open it in the file location;
- Then, type cmd in the location bar and press enter;
- Next, type the file name in cmd and press enter; Your program will run like this👆;
NOTE:- You can run this much in VS Code or IDLE.
This program simply assigns the location of the file to the script
variable(Any variable name can be used instead of script
). Now let’s see how can you give more than one variable.
from sys import argv
script, first = argv
print("The script is called:", script)
print("Your first variable is:", first)
To run this:-
- The first two steps are the same as before;
- In the third step type, the file name, then a space, and then the value of the variable without any spaces. However, you can give a string with spaces. Just like I did above.
Now, must have clearly got the difference between the input function and
argv
of sys.
The input function simply takes user input after you run the program.
And argv
is an argument variable that assigns values to variables before we run the program.
And a program containing the input function can run in any development environment whereas, a program having argv
might just run on the terminal.
Conclusion
In this post, we saw how to take user input and some advanced stuff related to it.
We also saw the difference between user input and argv
of sys
.
You got this post correct if you are able to solve the challenge below to some stretch.
Challenge🧗♀️
So, your challenge for the day is to make a Madlib generator.
Under this, you would need to make the user enter words for blanks without them knowing the story.
You can give them hints in the prompt like here use a verb, an adverb, a noun, a pronoun, etc. I hope you got the idea.
Please 🙇♀️ don’t cheat try to solve this on your own. Copying it from anywhere won’t develop your skills. Break this problem into small parts like:-
- First, store a story in a variable (multi-line string);
- Next, input the variables;
- Make use of ‘f-strings’ for combining variables and your story strings;
- Finally, print the original and the Madlib story.
This way you will improve your logic building and will also be able to make projects in near future. This will be an unbelievably helpful habit for you. Please give it a try.🙇♀️
Go fast. I am waiting. Comment your answers below.
This was it for the post. Comment below suggestions if there and tell me whether you liked it or not. Do consider sharing this with your friends.
See you in the next post till then have a great time.😊