在Python中从用户那里获取多个输入
在本教程中,我们将学习如何在Python中从用户那里获取多个输入。
用户输入的数据将为字符串格式。因此,我们可以使用split()方法划分用户输入的数据。
让我们从用户那里获取多个字符串。
示例
# taking the input from the user
strings = input("Enter multiple names space-separated:- ")
# spliting the data
strings = strings.split()
# printing the data
print(strings)输出结果
如果运行上面的代码,则将得到以下结果。
Enter multiple names space-separated:- Python JavaScript Django React ['Python', 'JavaScript', 'Django', 'React']
如果我们想接受多个数字怎么办?我们可以使用map和int函数将每个输入转换为整数。让我们来看一个例子。
示例
# taking the input from the user
numbers = input("Enter multiple numbers space-separated:- ")
# spliting the data and converting each string number to int
numbers = list(map(int, numbers.split()))
# printing the data
print(numbers)输出结果
如果运行上面的代码,则将得到以下结果。
Enter multiple numbers space-separated:- 1 2 3 4 5 [1, 2, 3, 4, 5]
结论
您可以根据需要修改代码并从用户那里获取输入。如果您对本教程有疑问,请在评论部分中提及它们。
热门推荐
1 训练狗狗工作
4 狗狗定点排便训练
5 狗狗训练吠叫
6 狗狗尿垫训练
7 狗狗训练座
8 狗狗训练攻略
9 狗狗训练记
10 做训练狗狗
11 狗狗训练词
12 狗狗谢谢怎么训练
13 狗狗训练如厕
14 狗狗太凶怎么训练
15 狗狗训练它
16 狗狗克制训练
17 训练基地狗狗
18 怎么样训练狗狗大小硬