Friday, June 23, 2023

Getting the user input and displaying output

Python has an in-built input() function that has an optional argument which is a prompt string. A prompt string can be a message for the user on the prompt that tells him/her about the value that he/she must provide.

During the execution of a program, if the interpreter encounters the input() method, it halts the program till the user provides input and hits the enter key.

Syntax

>>>input([prompt])

Look at the input() method shown as follows:

>>>input('Hi there! Do You live in New York?(Yes/No) :')

Here,

[prompt] = 'Hi there! Do You live in New York?(Yes/No) :'

You will notice that the cursor in front of the prompt string keeps blinking till the user enters a response, and hits the enter key. Till that happens the program execution is put to a halt.

The purpose of this method is to take input from the user and assign it to a variable. The following figure displays the user providing an input value ’Yes’ and pressing the enter key.

>>>user_input = input('Hi there! Do You live in New York?(Yes/No) :') 

Hi there! Do You live in New York?(Yes/No) : Yes

>>> print('the user says: ',user_input)

the user says : Yes

>>>

So, in the preceding code, the input() method is used to capture the response of the user which is a Yes and then the response is assigned to a variable by the name user_input,. Look at the print() method. It concatenates the string ‘The user says : ’ with the value stored in user_input variable and prints the value.

Let’s check the type of value obtained from the input() method (in other words, value of user_input).

>>>user_input = input('Hi there! Do You live in New York? (Yes/No) :')

Hi there! Do You live in New York?(Yes/No) :Yes

>>> type(user_input)

<class 'str'>

>>>

You may wonder what’s the need to check the type of user_input when clearly, the user is providing a string as input? You will understand the importance in the next example. 

Let’s try to use input() method to get user input to perform some arithmetic function. Look at the following code :

qty = input("How many apples do you have? : ")

price = input("What is the total cost? : ")

value_of_one = price/qty

print(value_of_one)

The output will be as follows:

How many apples do you have? : 80

What is the total cost? : 160

Also notice the error message: ‘unsupported operand type(s) for /: 'str' and 'str'’. This error was generated when an attempt was made to divide the variable price with the variable qty. The message is conveying that you made an attempt to divide a string variable with another string variable.

This means that although the user entered a numeric value, Python is treating it as a string value. This is because the input function always returns a string value. No matter what value is provided by the user, the input function will always treat it like a string.

So, in order to divide the price by quantity, you must convert both the values into the right data type.

qty = int(input("How many apples do you have? : "))

price = float(input("What is the total cost? : "))

value_of_one = price/qty

print(value_of_one)

The output will be as follows:

How many apples do you have? : 20

What is the total cost? : 256.50

12.825

In the preceding example, look at the expression int(input("How many apples do you have? : ")). This expression typecasts the value received by the user into an integer.







Share:

0 comments:

Post a Comment