Taking multiple inputs from user in Python
There are several ways to take multiple inputs from the user in Python. Here are some of the most common methods:
- Using the input() function multiple times:
name = input("Enter your name: ")
age = input("Enter your age: ")
- Using the split() method:
user_input = input("Enter your name and age: ")
name, age = user_input.split()
- Using the map() function:
name, age = map(str, input("Enter your name and age: ").split())
- Using a for loop:
user_input = []
for i in range(2):
user_input.append(input("Enter your name and age: "))
name, age = user_input
- Using the argparse module:
import argparse
parser = argparse.ArgumentParser()
parser.add_argument("name", help="Enter your name")
parser.add_argument("age", help="Enter your age")
args = parser.parse_args()
name = args.name
age = args.age
All of these methods will allow you to take multiple inputs from the user in Python.