Ask a Question in Python

Asking questions in Pyhton entails asking a question and accepting an answer from the user.

This is achieved using the input(<prompt>) function in Python 3 or raw_input(<prompt>) in Python 2. The question is passed to the function as <prompt>, and the user’s input is accepted. Once the answer has been typed, the user can exit the prompt by pressing the ENTER key.

In this article, we will learn how to create different types of questions in Python using the input() function.

Asking Open-Ended Questions

Open-ended questions are free-form questions that allow users to give any answer based on their knowledge. We can create an open question in Python with a snippet like the one shown below.

Output:

What is your name? Tomasz
Your answer is Tomasz

You can also use a for-loop to ask several open-ended questions.

questions = [
    "What is your name? ",
    "What is your registration number? ",
    "When were you born? ",
]
for question in questions:
    answer = input(question)
    print(answer)

Output:

What is your name? Tomasz
Tomasz
What is your registration number? 1595
1595
When were you born? 1995
1995

Asking Closed-Ended Questions

These questions can only be answered by picking the answer from a list of given options.

We will work on two types of closed questions – dichotomous and multiple choice. The former are questions with two answer options, but the latter may have more.

Asking dichotomous questions

In this case, we need to ask a question, accept the answer and check if the answer matches the two choices given. Here is how to ask a Yes or No question in Python.

Output (Take 1):

Do you like traveling? (yes/no) yes
You answered yes.

Output (Take 2):

Do you like traveling? (yes/no) success
You can only answer yes or no. Your answer is success.

The AskQuestion() function now accepts only “yes” or “no” as the valid answers – the feature for the closed-ended questions to have two options.

But there’s a problem. The code is too restrictive when it comes to answers. It only accepts the answers “yes” and “no” but not the varieties that may mean the same answer. For example, “yes”, “Yes”, “YES”, “y” or “1” can be used to mean the same thing, and “no”, “NO”, “n” or “0” to mean the contrary. Let’s add this to our function above and also allow the user to retry on invalid answers.

Output:

Do you like ice cream? (yes or no): 7
Your answer can only be yes/y/1 or no/n/0. You answered 7.
Do you like ice cream? (yes or no): y
You answered yes.

Asking Multi-Choice Questions

Like we did in dichotomous questions, we can use if-else statements to check our answers. In this case, we issue as many if-conditions as the number of possible choices. Let’s start with a simple example.

Output:

What is your favorite programming language? (Python, JavaScript, R) Python
You chose Python

The code above works for a simple question, but it may not be efficient if we have several choices. That form of asking a question may also not work for multiple-choice questions with right or wrong answers. For example, asking the question 2*3=? with the options being (8, 6, 5). In this case, we need to check not only the answer to come from the options but also the correctness of the answer given. Let’s modify the code above to do that.

Output:

Question:  4*8+6=?
Choices:
A : 56
B : 52
C : 80
D : 38
Type your answer here ['A', 'B', 'C', 'D']: D
Correct! You chose D
Question:  Brazil is located in?
Choices:
A : North America
B : South America
C : Asia
Type your answer here ['A', 'B', 'C']: A
You chose: A. The correct answer is B:South America
Question:  What is your programming language?
Choices:
A : Python
B : JavaScript
C : Rust
D : R
E : Java
Type your answer here ['A', 'B', 'C', 'D', 'E']: A
You chose: Python

Conclusion

This article discussed how to ask questions in Python – open and closed questions. We covered different examples to show how to ask questions in the correct way using the input() function. After going through the post, you should be able to easily tweak the code to fit your used case.