1个输入分隔中的3个值(数字).的Python 3 [英] 3 values (numbers) in 1 input separation. Python 3

查看:182
本文介绍了1个输入分隔中的3个值(数字).的Python 3的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我现在正在编写代码,因为它的一部分需要在一行中要求用户输入3个不同的数字(每个数字可以是任意数字). 假设我要求用户输入,然后他输入:"31 722 9191". 数字之间必须有一个空格.您将如何分隔这些数字并为每个数字分配一个变量.因此,例如31将是"A",722将是"B",依此类推... 到目前为止,我得到了什么:

I'm working on a code right now that a part of it requires to ask the user for 3 different numbers in one line ( could be any number of digits in each number). Say I ask the user for the input and he enters : "31 722 9191". A space is required between the numbers. How would you go about separating these numbers and assigning a variable to each one of them. So for example 31 would be "A", 722 would be "B" and so on... What I've got so far:

user_input = input(" Please enter the numbers: ")

谢谢!

推荐答案

结合使用拆分和序列拆包.

Use a combination of split and sequence unpacking.

user_input = user_input(" Please enter the numbers: ")
a, b, c = user_input.split()

split将采用您的数字字符串,例如"x y z",并将其转换为字符串中的元素列表,其中元素是字符串中所有用空格分隔的单词.因此split将为输入'x y z'产生字符串['x','y','z'].

split will take your string of numbers, say "x y z", and turn it into a list of elements in the string where the elements are all the words in the string that are separated by spaces. Thus split will yield the string ['x', 'y', 'z'] for input 'x y z'.

由于列表是序列的一种形式,因此可以将其元素拆包"并分配给您选择的变量列表.

Since a list is a form of sequence, its elements can be "unpacked" and assigned to a list of variables of your choosing.

这篇关于1个输入分隔中的3个值(数字).的Python 3的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆