F.E. PPS Unit 1 Practical 3 solution

Practical 3: Program to Read Variables from the User

Problem Statement:

Write a Python program to read different types of variables from the user (such as integer, float, string, etc.) and display them.

Solution Code:

				
					# Reading and displaying different types of variables from the user
 
# Input integer
int_var = int(input("Enter an integer: "))
print("Integer entered:", int_var)
 
# Input float
float_var = float(input("Enter a floating-point number: "))
print("Float entered:", float_var)
 
# Input string
str_var = input("Enter a string: ")
print("String entered:", str_var)
 
# Input boolean (converted from user input)
bool_var = input("Enter True or False: ")
bool_var = bool_var.lower() == "true"  # Convert string input to boolean
print("Boolean entered:", bool_var)

				
			

Output:

  • The program will prompt the user to enter an integer, float, string, and boolean value.
  • After input, the program will display the entered values along with their types. Example output for inputs:
				
					
Enter an integer: 10
Integer entered: 10
Enter a floating-point number: 2.2
Float entered: 2.2
Enter a string: the boy
String entered: the boy
Enter True or False: Flase
Boolean entered: False
Process finished with exit code 0
				
			

Explanation:

Below is the explanation of F.E. PPS Unit 1 Practical 4 solution where we have written Python Program to Read Variables from the User

  1. Reading an Integer:
    • We use input() to get input from the user, but since input() returns a string, we convert it to an integer using int().
    • The user enters a number, which is then printed after conversion.
  2. Reading a Float:
    • The same process is applied to a floating-point number. The user inputs a decimal number, and we convert it to a float using float().
  3. Reading a String:
    • For string input, input() directly reads the value as a string. We simply print the string entered by the user.
  4. Reading a Boolean:
    • The user is asked to input either True or False as a string.
    • We use .lower() to ensure that the input is case-insensitive, and compare it to “true”. If it matches, we set the boolean value to True, otherwise to False.

Key Concept Learned:

  • Reading input from users using the input() function.
  • Converting user input into appropriate types such as integer (int()), float (float()), and boolean conversion.
  • Understanding how to handle different types of inputs in Python.
Tech Amplifier Final Logo