找回密码
 立即注册

扫一扫,访问微社区

QQ登录

只需一步,快速开始

查看: 3860|回复: 4

[求助] 新手求教!输入参数构成嵌套字典的问题

1

主题

3

帖子

3

积分

贫民

积分
3
littlecircle 发表于 2016-10-22 15:29:08 | 显示全部楼层 |阅读模式
捕获.PNG
我想设计一个输入key 和 value构造嵌套字典的函数,但是运行的时候一直在循环输入key 和value,不知道该怎么改

回复

使用道具 举报

1419

主题

1891

帖子

291

积分

侠客

积分
291

最佳新人热心会员默默耕耘

whydo1 发表于 2016-10-22 20:36:17 | 显示全部楼层
dic1[key1] = value1这一句缩进到和上一句相同
另外在和while同样缩进的地方加一句
dic[key] = dic1
python3.4.4, win10
回复 支持 反对

使用道具 举报

1

主题

3

帖子

3

积分

贫民

积分
3
littlecircle  楼主| 发表于 2016-10-25 21:10:01 | 显示全部楼层
改过了可是还是不行。。
print('please input a dict')
def input_dict():
dic = {}
key = input('Input key:')
i=input('nest? yes/no')
if i == 'yes':
     dic1 = {}
     while True:
       key1 = input('Input key:')
       if key == '':
         break
       value1 = input('Input value:')
       dic1[key1] = value1
     dic[key]=dic1
else:
     while True:
         value = input('Input value')
         key = input('Input key')
         if key =='':
           break
         dic[key] = value
return dic
dict=input_dict()
print(dict)
   但是如果只构成一个没有嵌套的字典是可以执行的
dic = {}
while True:
    key = input('Input key:')
    if key == '':
        break
    value = input('Input value:')
    dic[key] = value
print(dic)
回复 支持 反对

使用道具 举报

1

主题

3

帖子

3

积分

贫民

积分
3
littlecircle  楼主| 发表于 2016-10-25 21:11:38 | 显示全部楼层
whydo1 发表于 2016-10-22 20:36
dic1[key1] = value1这一句缩进到和上一句相同
另外在和while同样缩进的地方加一句
dic[key] = dic1 ...

改过了可是还是不行。。
print('please input a dict')
def input_dict():
dic = {}
key = input('Input key:')
i=input('nest? yes/no')
if i == 'yes':
     dic1 = {}
     while True:
       key1 = input('Input key:')
       if key == '':
         break
       value1 = input('Input value:')
       dic1[key1] = value1
     dic[key]=dic1
else:
     while True:
         value = input('Input value')
         key = input('Input key')
         if key =='':
           break
         dic[key] = value
return dic
dict=input_dict()
print(dict)
   但是如果只构成一个没有嵌套的字典是可以执行的
dic = {}
while True:
    key = input('Input key:')
    if key == '':
        break
    value = input('Input value:')
    dic[key] = value
print(dic)
回复 支持 反对

使用道具 举报

1419

主题

1891

帖子

291

积分

侠客

积分
291

最佳新人热心会员默默耕耘

whydo1 发表于 2016-10-25 21:36:37 | 显示全部楼层
  1. print('please input a dict')
  2. def input_dict():
  3.     dic =
  4. {}
  5.     key = input('Input key:')
  6.     i=input('nest?
  7. yes/no:')
  8.     if i == 'yes':
  9.         
  10. dic1 = {}
  11.          while True:
  12.   
  13.           key1 = input('嵌套字典键:')
  14.   
  15.           if key1 == '':
  16.   
  17.               break
  18.   
  19.           value1 = input('嵌套字典值:')
  20.   
  21.           dic1[key1] = value1
  22.   
  23.        dic[key]=dic1
  24.     else:
  25.   
  26.        while True:
  27.         
  28.      value = input('Input value')
  29.      
  30.         key = input('Input key')
  31.      
  32.         if key =='':
  33.      
  34.           break
  35.      
  36.         dic[key] = value
  37.     return
  38. dic
  39. dict=input_dict()
  40. print(dict)
复制代码

python3.4.4, win10
回复 支持 反对

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

快速回复 返回顶部 返回列表