Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/365.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/list/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 正在尝试为.append使用多个参数,无论如何,如何?_Python_List_Append - Fatal编程技术网

Python 正在尝试为.append使用多个参数,无论如何,如何?

Python 正在尝试为.append使用多个参数,无论如何,如何?,python,list,append,Python,List,Append,我试图将一组对象作为单个对象添加到列表的末尾。我有没有办法做到这一点 我已经尝试为.append使用多个参数,并尝试搜索其他函数,但到目前为止还没有找到任何参数 yourCards = [] cards =["Ace","Two","Three","Four","Five","Six","Seven","Eight","Nine","Ten","Jack","Queen","King"] suits = ["Hearts","Diamonds","Clubs","Spades"] yourCa

我试图将一组对象作为单个对象添加到列表的末尾。我有没有办法做到这一点

我已经尝试为.append使用多个参数,并尝试搜索其他函数,但到目前为止还没有找到任何参数

yourCards = []
cards =["Ace","Two","Three","Four","Five","Six","Seven","Eight","Nine","Ten","Jack","Queen","King"]
suits = ["Hearts","Diamonds","Clubs","Spades"]

yourCards.append(cards[random.randint(0,12)],"of",suits[random.randint(0,3)])
我希望列表中有一个新元素,简单地说是“两颗心”等等,但我收到了这个错误:

TypeError: append() takes exactly one argument (3 given)

您正在发送多个参数而不是字符串。将参数格式化为字符串。另外,
random.choice()
比这里的
random.randint()
更好,如下所述:@JaSON

3.6+使用
f字符串

yourCards.append(f"{random.choice(cards)} of {random.choice(suites)}")
yourCards.append("{} of {}".format(random.choice(cards), random.choice(suites)))
使用
.format()

yourCards.append(f"{random.choice(cards)} of {random.choice(suites)}")
yourCards.append("{} of {}".format(random.choice(cards), random.choice(suites)))
字符串串联

yourCards.append(str(random.choice(cards)) + " of " + str(random.choice(suites)))
#You likely don't need the str() but it's just a precaution
改进Alex的
join()
approch

' of '.join([random.choice(cards), random.choice(suites)])

您为
append
提供了三个参数,但
append
只接受一个参数。注意参数列表中的逗号?您的意思是要构建字符串
“两颗心”
?通过单独的辩论如何实现这一点?在将其传递给append之前执行此操作。还要注意的是,
random.choice
的存在,没有什么可以阻止你拥有同一张卡的多个副本。你可以使用
extend
来代替:
yourCards.append((cards[random.randint(0,12)],“of”,suites[random.randint(0,3)])
。也许你是从
print
功能中得到这个想法的?它将传递给它的参数与空格连接起来,但函数调用通常不是这样工作的;那只是为了方便打印。当您有两个字符串对象时,您可以使用“+y的
+
操作符:
x+”将它们连接起来。为什么不使用
+
将它们添加到一起呢?比如你的卡片。附加(卡片[random.randint(0,12)]+'of'+套装[random.randint(0,3)])
?我也喜欢这个。但是可以使用“.of.”加入([cards[random.randint(0,12)],suits[random.randint(0,3)])进行改进。谢谢!我喜欢按顺序排列“of”,这样代码也会形成一个句子。我想最好使用
random.choice(cards)
random.choice(suites)
@JaSON。谢谢