[英]Trying to printing the dictionary values by taking a list of strings and given user input
我正在嘗試使用 output 將值打印為字典(如鍵和值),如{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)**
當我執行代碼時,上面提到了 output,下面是代碼片段
~/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)
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.