2013-07-24 57 views
0

我想在输入他的名字时看到johns号码。 首先要做到这一点:Python字典中if if else

def liste_kontrol(liste): 
    liste=raw_input("Name pls:") 
    if liste=="john": 
     print "now u seeng on list",john,"number." 
    else: 
     print "cant found" 
liste={"john":002050505",} 
liste_kontrol(liste) 

,也许下一个:

def liste_kontrol(liste): 
    liste=raw_input("Name pls:") 
    if liste=="john" or "jack: 
     print "now u seeng on list", john, or jack, "number." 
    else: 
     print "cant found." 
liste= {"john":"002050505","jack":"0551282"} 
liste_kontrol(liste) 
+3

你得解释一下你的自我 –

+0

笏。笏? waat! – kindall

+0

去吧!你能行的! – Marcin

回答

0

好像你正在试图做到这一点:

def liste_kontrol(liste): 
    name=raw_input("Name pls:") 
    if name in liste: 
     print "now u seeng on list", name, liste[name] 
    else: 
     print "cant found." 
liste= {"john":"002050505","jack":"0551282"} 
liste_kontrol(liste) 
+0

多数民众赞成它。谢谢! – jander

1

理想情况下,你应该使用异常处理在这里,并有向上在列表前面的查询,请尝试:

names = { 
    "john": "002050505", 
    "jack": "0551282" 
} 

name = raw_input('Enter a name:') 

try: 
    print "{}'s number is {}".format(name, names[name]) 
except KeyError as e: 
    print "Couldn't find {}".format(name) 

这样可避免必须在有效名称中编程为if条件的一部分,并预先检查是否有名称的相关值。

+0

非常感谢你,但是我在python中很新,只需要为我所知道的写下它。 – jander