我试图将值打印为字典(如键和值),输出如{0: 'Rama', 1: 'Sita', 2: 'Ravana'}
。
尝试代码:
def details():
Stu_Rec = {}
Names = list()
print("Enter the Id :n")
Id = int(input())
for i in range(0,Id):
print("nEnter the Name ")
Name = input()
Names.append(Name)
for i in range(0,Id):
for x in Names:
Stu_Rec[i] = x
return Stu_Rec
if __name__ == "__main__":
Details = details()
print(Details)**
当我执行代码,输出是上面所提到的,下面是代码片段
~/Python-3$ python details.py
Enter the Id :
3
Enter the Name
rama
Enter the Name
sita
Enter the Name
Ravana
{0: 'Ravana', 1: 'Ravana', 2: 'Ravana'}**
建议我正在尝试的代码有什么问题?
试试这个
def details():
Stu_Rec = {}
Names = list()
print("Enter the Id :n")
Id = int(input())
for i in range(0,Id):
print("nEnter the Name ")
Name = input()
Names.append(Name)
for i in range(0,Id):
Stu_Rec[i] = Names[i]
return Stu_Rec
if __name__ == "__main__":
Details = details()
print(Details)