lis中冒号的Python语法错误

2024-06-16 12:53:23 发布

您现在位置:Python中文网/ 问答频道 /正文

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

字典

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])

Tags: of用户程序number列表piece字典that
1条回答
网友
1楼 · 发布于 2024-06-16 12:53:23

您忘了在dict中每个条目的末尾都有一个逗号。dict使用大括号“{...}

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',      
    }

相关问题 更多 >