小编Har*_*rry的帖子

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])
Run Code Online (Sandbox Code Playgroud)

python syntax list

3
推荐指数
1
解决办法
1773
查看次数

有多个"if"语句

我正在尝试创建一个基本的登录程序,要求用户输入用户名和密码,并在日志文件中记录任何正确或错误的文本.我遇到的问题是,当我尝试使用多个用户名和密码组合时,我会遇到语法错误.

#login program
output = open("logfile.txt", 'a')
login = input("please enter the correct username and password\n")
a = "username password"
b = "username1 password1"

if a in login:
    print("successfully logged in")
    import time
    localtime = time.asctime(time.localtime(time.time()))
    output.write("Somebody successfully logged in at ")
    output.write(localtime)
    output.write("\n")
    output.close()
if b in login:
    print("successfully logged in")
    import time
    localtime = time.asctime(time.localtime(time.time()))
    output.write("Somebody successfully logged in at ")
    output.write(localtime)
    output.write("\n")
    output.close()

else:
    print("access denied.")
    import time
    output = open("logfile.txt", "a")
    localtime = time.asctime(time.localtime(time.time())
    output.write("Somebody unsuccessfully …
Run Code Online (Sandbox Code Playgroud)

python syntax if-statement

0
推荐指数
1
解决办法
114
查看次数

标签 统计

python ×2

syntax ×2

if-statement ×1

list ×1