如何阻止一个字母一个字母地打印出随机项目

时间:2019-03-26 22:57:36

标签: python

我的游戏中存在随机物品掉落系统的问题。

这是我的代码:

import random

#Potato heals +5
potato = "Potato"
apple = 1
apple_name = "apple"

#is_damage_2
rusted_sword = "Rusted shortsword"

#Worth $80 - 10 can make Goldblade
gold_ingot = "Gold ingot"

#Worth $120
sapphire = "Sapphire"

random_drop = [sapphire,potato,gold_ingot,rusted_sword]

inventory = [apple_name, apple,]

rand_item = random.choice(random_drop)

inventory += rand_item

print(inventory)

当它打印库存时...添加了随机项目,但它的字母拼写如下:P,o,t,a,t,o。

我希望将其拼写为:“土豆”

我只学习Python几周了,非常困惑。任何帮助将不胜感激!

  • Justin

1 个答案:

答案 0 :(得分:3)

使用append()方法,如下所示:

inventory.append(rand_item)

有关+=对可迭代对象的作用,请参见以下答案。

Python append() vs. + operator on lists, why do these give different results?

Why does += behave unexpectedly on lists?