如何在Python 3.4中编写一个密码程序?

2024-04-25 22:58:05 发布

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

我目前的用户名和密码相当于在代码中找到的一个。这就是我目前所拥有的:

import os

#Must Access this to continue.
def main():
    while True:
        UserName = input ("Enter Username: ")
        PassWord = input ("Enter Password: ")

        if UserName == Bob and PassWord == rainbow123:
            time.sleep(1)
            print ("Login successful!")
            logged()

        else:
        print ("Password did not match!")

def logged():
    time.sleep(1)
    print ("Welcome to ----")

main()

运行代码后,收到以下错误:

Traceback (most recent call last):
  File "C:\Users\Austin\Desktop\oysterDev\oysterDev.py", line 23, in <module>
    main()
  File "C:\Users\Austin\Desktop\oysterDev\oysterDev.py", line 11, in main
    if UserName == Bob and PassWord == rainbow123:
NameError: name 'Bob' is not defined

有人知道我做错了什么吗?或者我在哪里可以找到像这样适用于Python3.4的代码?谢谢!


Tags: andto代码inputifmaindefusername
1条回答
网友
1楼 · 发布于 2024-04-25 22:58:05

现在您正在检查正确的密码和用户:

if UserName == Bob and PassWord == rainbow123:

如果没有引号,python希望bobrainbow123是定义的变量。因为它们没有定义,所以它抛出一个NameError

只需将这些值用引号括起来:

import os
import time
#Must Access this to continue.
def main():
    while True:
        UserName = input ("Enter Username: ")
        PassWord = input ("Enter Password: ")

        if UserName == 'Bob' and PassWord == 'rainbow123':
            time.sleep(1)
            print ("Login successful!")
            logged()

        else:
            print ("Password did not match!")

def logged():
    time.sleep(1)
    print ("Welcome to ----")

main()

相关问题 更多 >