下面是字典:
std[name]={'uid':uid ,'age':age,'subject':{'math':math,'phy':phy,'chem':chem}}
输出应为:
列出name
和uid
。
这是我的代码:
std={}
i=0
n=int(raw_input("How many Entries you have to fill:"))
for i in range (n):
name=raw_input("Enter name:")
uid=raw_input("ID:")
age=raw_input("Age:")
print("----Marks----")
math=raw_input("Maths:")
phy=raw_input("Physics:")
chem=raw_input("Chemistry:")
std[name]={'Uid':uid,'Age':age,'subject'{'math':math,'phy':phy,'chem':chem}}
print('\n')
print(std['name']['uid'])
答案 0 :(得分:0)
python 3.x
中的:
for k,v in std.items():
print(k,v['Uid'])
python 2.x
中的:
for k,v in std.iteritems():
print(k,v['Uid'])
python 3.x
代码:
std={}
n=int(input("How many Entries you have to fill:"))
for i in range (n):
name=input("Enter name:")
uid=input("ID:")
age=input("Age:")
print("----Marks----")
math=input("Maths:")
phy=input("Physics:")
chem=input("Chemistry:")
std[name]={'Uid':uid,'Age':age,'subject':{'math':math,'phy':phy,'chem':chem}}
print('\n')
for k,v in std.items():
print(k,v['Uid'])
两个不同名称的输入和结果将是:
How many Entries you have to fill:2
Enter name:John
ID:15895
Age:18
----Marks----
Maths:15
Physics:15
Chemistry:15
Enter name:Beck
ID:52256
Age:54
----Marks----
Maths:52
Physics:52
Chemistry:21
John 15895
Beck 52256