【问题标题】:Python - Syntax error on colon in listPython - 列表中冒号的语法错误
【发布时间】:2013-10-21 01:48:28
【问题描述】:

我一直在尝试创建一个简单的字典来定义用户输入的单词。在定义字典和它的单词之后,我试图打印输入单词的定义。出于某种原因,当我尝试运行该程序时,列表中的冒号出现语法错误。我不确定如何解决这个问题,我知道有更简单的方法可以做到这一点,但我正在尝试练习使用列表。 到目前为止的代码如下:

字典

dic1 = [
    'bug':'A broken piece of code that causes a program to stop functioning'
    'string':'A piece of text'
    'integer':'A whole number'
    'float':'A decimal number'
    'function':'A block of organized and clean code that performs a task/action'
    'syntax':'A set of rules that says how a program will be coded'      
    ]

q = input("What coding related word do you want defined?")
if q in dic1:
    print(dic1[q])

【问题讨论】:

    标签: python list syntax


    【解决方案1】:

    您忘记了字典中每个条目末尾的逗号。字典使用花括号“{...}

    dic1 = {
        'bug':'A broken piece of code that causes a program to stop functioning',
        'string':'A piece of text',
        'integer':'A whole number',
        'float':'A decimal number',
        'function':'A block of organized and clean code that performs a task/action',
        'syntax':'A set of rules that says how a program will be coded',      
        }
    

    【讨论】:

    • 谢谢! :D 我知道我错过了一些简单的事情
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2014-12-12
    • 1970-01-01
    • 1970-01-01
    • 2016-02-04
    • 1970-01-01
    • 2017-07-08
    • 2018-10-08
    相关资源
    最近更新 更多