将变量输入字典

时间:2016-02-12 14:05:42

标签: variables if-statement python-3.4

这是我的代码,或者更确切地说是我遇到问题的代码部分。

问题出在if表达式,我试图检查一个值是否已经存在于字典中(实际代码位于更大的while循环中,用于用户界面),但是当它第二次运行时它将始终进入else部分,我假设因为变量名已经存在于字典中。

    Dict1 = {#empty dictionary}
    completedList = (#list variable containing “true” and “false”)
    name = input(“enter your name”)
    score = completedList.count("true")
    callback = 'cont'
    while callback == 'cont':
        if name not in dict1 == True:
            dict1 = [name] = score
            callback = ""
        else:
            print("that name is already in use \n please choose another")
            name = input("enter a name")

我是学生,所以代码越简单越好。

1 个答案:

答案 0 :(得分:0)

    dict1 = {'er'}
    completedLis=()
    name = input("enter your name")
    score = completedLis.count("true")
    callback = 'cont'
    while (callback == 'cont'):
        if (name not in dict1):
            dict1 = name = score
            callback = ''
        else:
            print("that name is already in use \n please choose another")
            name = input("enter a name")

我希望这可以解决你的问题,如果你输入er(dict1 ='er')它会转到其他地方,或者如果你输入其他任何东西它会起作用