1 243
Repost from Leetcode with dani
How to get input from the user Python :
1. Getting User Input:
To get input from the user, you can use the
input() function in Python. It reads a line of text entered by the user and returns it as a string.
Example:
python
name = input("Enter your name: ")
print("Hello, " + name)
2. Converting Input to Other Data Types:
By default, input() returns the user input as a string. If you need to convert it to other data types like integer or float, you can use type casting.
Example:
python
age = int(input("Enter your age: "))
height = float(input("Enter your height (in meters): "))
3. Prompting for Multiple Inputs:
You can prompt for multiple inputs by calling input() multiple times and storing the results in different variables.
Example:
python
num1 = int(input("Enter first number: "))
num2 = int(input("Enter second number: "))
sum = num1 + num2
print("Sum:", sum)
Let me know if you have any further questions.22 Apr 2024, 13:17
