我的游戏中存在随机物品掉落系统的问题。
这是我的代码:
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几周了,非常困惑。任何帮助将不胜感激!
答案 0 :(得分:3)
使用append()方法,如下所示:
inventory.append(rand_item)
有关+=
对可迭代对象的作用,请参见以下答案。
Python append() vs. + operator on lists, why do these give different results?