Taking Input from the user

In order to take input from user, we need a function called input()

Let’s say we want to accept the user’s age. Then we will use the input() function as follows:

If we run this code, we get the output as follows:

C:\practice>py 1.py

Enter your age: 

After entering the value, the value will be stored in the age variable. But the input() function converts the entered data into string. 

This can be an undesired phenomenon if we are dealing with numeric values. To solve this problem, we have to convert the value back to a number using the int() or float() function as required.

If we are dealing with integers, we will use the int() function and for floating-point numbers, we will use the float() function.

Interview Questions

Ans. The primary function for getting user input in Python is input().

Ans. Use the input() function along with a prompt (e.g., “Enter your age: “) to guide the user.

Store the input received from the user into a variable, such as age.

Ans. The input() function always returns the user’s input as a string. This can be problematic when working with numerical values (integers or floats) because you’ll need to convert the string back to its appropriate numeric type before performing calculations or comparisons.

Ans. To convert a string representing an integer into an actual integer, use the int() function.

To convert a string representing a floating-point number into a float, use the float() function.

Coding Problems

Ans. 

age_str = input(“Enter your age: “)

age = int(age_str)  # Convert age to integer

current_year = 2024  # Assume current year (can be obtained dynamically)

birth_year = current_year – age

print(“You were likely born in:”, birth_year) 

Ans. 

length_str = input(“Enter the length of the rectangle: “)

width_str = input(“Enter the width of the rectangle: “)

length = float(length_str)

width = float(width_str)

area = length * width

print(“The area of the rectangle is:”, area)

Ans. 

celsius_str = input(“Enter the temperature in Celsius: “)

celsius = float(celsius_str)

fahrenheit = (celsius * 9/5) + 32

print(“The temperature in Fahrenheit is:”, fahrenheit)

Ans.

num1_str = input(“Enter the first number: “)

num2_str = input(“Enter the second number: “)

num1 = float(num1_str)

num2 = float(num2_str)

sum = num1 + num2

average = sum / 2

print(“The sum of the numbers is:”, sum)

print(“The average of the numbers is:”, average)

Ans.

name = input(“Enter your name: “)

print(“Hello,”, name + “!”)  

Ans.

number_str = input(“Enter a number: “)

number = float(number_str)

square = number * number

print(“The square of the number is:”, square)

Code snippet based questions

Ans.

The input() function prompts the user to enter their age and stores the input as a string in the age_str variable.

The int() function is used to convert the string stored in age_str into an integer data type, allowing for numerical calculations later.

If the user enters “35”, the code will print “Next year, you will be: 36”.

Ans.

If the user entered “twenty”, the code would raise a ValueError because the float() function cannot convert the string “twenty” into a floating-point number.

You can modify the code to strip the dollar sign before converting to float:

price_str = input(“Enter the price of the item: “)

price_str = price_str.replace(“$”, “”)  # Remove dollar sign

price = float(price_str)

# … (rest of the calculation)

num1_str = input(“Enter the first number: “)

num2_str = input(“Enter the second number: “)

result = num1_str + num2_str

print(“The result is:”, result)

Ans. The output will be “The result is: 1020” because the + operator performs string concatenation when both operands are strings. To get the numerical sum, you need to convert the input strings to numbers using int() or float().