首页 > 解决方案 > I created a program, I input keys and values to add to a dictionary but it doesn't work properly

问题描述

liste = []
while True:
    key = input("key: ")
    val = input("val: ")
    words = ('"{}":"{}"'.format(key,val))
    liste.append(words)
    if len(liste)>=16: #I have 16 words per page
        print(liste)

Here is my code. It is working but when I print the list, it is like:

liste = ['"key1","val1"','"key2","val2"','"key3","val3"']

As you can see, there are prime marks (') at the beginning and end of every item. So if I copy and paste those keys and values to my dictionary it won't work. How can I delete these prime marks from my list?

标签: pythonlistdictionarywhile-loopformat

解决方案


To create a dictionary use {key:val}

Ex:

liste = []
while True:
    key = input("key: ")
    val = input("val: ")
    words = {key: val} 
    liste.append(words)
    if len(liste)>=16: #I have 16 words per page
        print(liste)

推荐阅读