如何在python中使用raw_input输入值并将它们添加到字典中的字典中?

时间:2013-12-23 01:27:55

标签: python dictionary raw-input

我如何获得此代码:

ans = raw_input('Enter Amount of Players: ').lower()
if ans == '2':
    a = raw_input('What is Player 1 named:')
    b = raw_input('What is Player 2 named:')
    cf={a:{}, b:{}}
    p1 = raw_input('What is Player 1 city named:')  
    p2 = raw_input('What is Player 2 city named:') 
    cf[a[p1]] = '50'
    cf[b[p2]] = '50'

如果我为玩家1输入'matt',为玩家2输入'nick',为玩家1城市输入'nyc',为玩家2城市输入'la',则产生类似的东西:

 {'matt':{'nyc':50}, 'nick':{'la':50}}

而不是错误代码?

1 个答案:

答案 0 :(得分:1)

这应该有效:

 cf.__getitem__(a).__setitem__(p1,50)
 cf.__getitem__(b).__setitem__(p2,50)