打印获取文本和[],而[]应包含数据 [英] Print gets a text and [] while [] should contain data

查看:76
本文介绍了打印获取文本和[],而[]应包含数据的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有这个变量来跟踪你在哪个房间,大厅是1,楼梯是2,走廊是3,等等。它通过raw_input的正常数学更新



I have this variable to track in which room you are, lobby is 1, Stairs is 2, hallway is 3, etc. it gets updated by normal math from raw_input

currenKamer = 1





这是显示您可以移动的位置的代码,但它返回 hier kan je naartoe [] 。我缺少什么?





This is the code which displays where you are in where you could move, but it returns "hier kan je naartoe []". What am I missing?

def showRoom(self):
    #kamer
    print(20 * "-")
    print("Hier ben je: " + kamers[currentKamer]["naam"])
    print("hier kan je naartoe: %s ") % [i['naam'] for i in kamers.values() if i.get('currentKamer',None) ]
    #leuk zinnetje per kamer
    if "note" in kamers[currentKamer]:
        print (20 * "-")
        print("--" + kamers[currentKamer]["note"])
    print(20 * "-")





我的尝试:





What I have tried:

[i['naam'] for i in kamers.values() if i.get == currenKamer ]






and

[i['naam'] for i in kamers.values() if i.get(currentKamer) ]



两者都只返回[]


Both return nothing but []

推荐答案

基于在您的评论中的房间布局,您的代码以及我说荷兰语的能力,看起来您想获得所有不是naam或note的值,然后根据这些ID从kamers中获取名称。



我会重新调整布局,使事情变得简单。我推荐这样的布局:

Based on your room layout in your comment, your code, and my ability to speak Dutch, it looks like you want to get all values that are not "naam" or "note" and then fetch the names from "kamers" based on these IDs.

I would restructure the layout to make things less complicated. I recommend a layout like this:
kamers = {
    1: { "naam": "de Lobby", "adjacent": [2, 3] }, ...
}

现在你可以使用相邻的值,找出你可以去的房间。



你可以这样做:

Now you can just use the adjacent value to figure out what rooms you can go to.

You can do that like this:

[kamers[i]['naam'] for i in kamers[currentKamer]['adjacent']]

for i in kamers [currentKamer] ['adjacent'] 迭代在'相邻'列表中的每个整数以currentKamer为关键的kamers中的值。然后 kamers [i] ['naam'] 选择房间的名称,密钥为 i

for i in kamers[currentKamer]['adjacent'] iterates over each integer in the 'adjacent' list of the value in "kamers" with 'currentKamer' as key. Then kamers[i]['naam'] selects the name for the room with key i.


这篇关于打印获取文本和[],而[]应包含数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆