如何读取用户命令输入并将部件存储在变量中

问题描述 投票:0回答:2

所以我们假设用户输入!give_money user#5435 33000

现在我想把那个user#543533000存储在变量中。

我怎么做?也许这很简单,但我不知道。

如果您需要更多信息,请发表评论。

谢谢!

python discord.py
2个回答
1
投票

拆分空格上的输入并提取第二个和第三个元素:

parts = input().split()
user = parts[1]
numb = parts[2]

虽然将解包变量变为Pythonic(使用常规下划线丢弃第一个):

_, user, numb = input().split()

为了进一步说明,input.split()返回在传递给函数的分隔符中拆分的子列表的列表。但是,当没有输入时,字符串将在空格上分割。

为了感受,请观察:

>>> 'hello there bob'.split()
['hello', 'there', 'bob']
>>> 'split,on,commas'.split(',')
['split', 'on', 'commas']

然后解压缩只是将变量分配给列表中的每个元素:

>>> a, b, c = [1, 2, 3]
>>> a
1
>>> b
2
>>> c
3

2
投票
list_of_sub_string=YourString.split()
print(list_of_sub_string[-1])  #33000
print(list_of_sub_string[-2])  #user#5435
© www.soinside.com 2019 - 2024. All rights reserved.