如何在 Python 3.4 中制作密码程序?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/26940297/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-19 01:12:02  来源:igfitidea点击:

How do I make a password program in Python 3.4?

pythonpasswords

提问by oiestin

I am currently username and password was equivalent to one found in the code. This is what I have so far:

我目前的用户名和密码相当于在代码中找到的。这是我到目前为止:

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

After I run the code, I receive this error:

运行代码后,我收到此错误:

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

Does anyone know what I am doing wrong? Or where I could find some code that works like this for Python 3.4? Thanks!

有谁知道我做错了什么?或者我在哪里可以找到一些像这样适用于 Python 3.4 的代码?谢谢!

采纳答案by agconti

Right now your checking for a correct password and user with:

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

if UserName == Bob and PassWord == rainbow123:

Without quotes python expects that boband rainbow123are defined variables. Since they're not defined it throws a NameError.

没有引号 python 期望它bob并且rainbow123是定义的变量。由于它们没有定义,它会抛出一个NameError.

Just enclose those values in quotes:

只需将这些值括在引号中:

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