我正在为排队的朋友制作一个机器人游戏。我是个初学者。我试图在json中调用一个包含string+integer的对象。我四处看了看,但似乎什么都不符合我的需要。最好的/简单的解决方案是什么?你知道吗
我的代码是业余的,请对我放轻松。:页 我试图通过Json,“Name”+“Stat”来提取Python。 现在它只提取“Name”并随机选择一个项目。有没有办法选择项目+统计,显示项目和计算统计?谢谢。你知道吗
Python 3:
if text == 'FIGHT':
with open('items.json', 'r') as f:
data = json.load(f)
armor1 = [v for d in data['armor'] for k,v in d.items() if k == 'name']
weapon1 = [v for d in data['weapon'] for k,v in d.items() if k == 'name']
magic1 = [v for d in data['magic'] for k,v in d.items() if k == 'name']
armor2 = random.choice(armor1)
weapon2 = random.choice(weapon1)
magic2 = random.choice(magic1)
calc = add(int(armor2), int(weapon2), int(magic2))
line_bot_api.reply_message(
event.reply_token,
TextSendMessage('Armor = ' + (armor2)),
TextSendMessage('Weapon = ' + (weapon2)),
TextSendMessage('Magic = ' + (magic2)),
TextSendMessage('You have a score of ' + str(calc) + '.'),
TextSendMessage('Waiting for next opponent...')
)
Json:
"armor": [
{
"name":"Cardboard armor 10 DEF" ,
"stats":"10" },
{
"name":"Plastic armor 20 DEF" ,
"stats":"20" },
{
"name":"Rubber armor 30 DEF" ,
"stats":"30" },
{
"name":"Metal armor 40 DEF" ,
"stats":"40" },
{
"name":"Indestructable armor 50 DEF" ,
"stats":"50" }
],
在尝试了一切之后。。解决方案是:
感谢所有人,特别感谢帮助我的朋友Sae。:)
相关问题 更多 >
编程相关推荐