如何搜索文件中的变量,textwrapper没有要查找的属性

2024-09-27 09:29:23 发布

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

我正在编写一个骰子游戏,只允许经过身份验证的用户玩,我有一个附加和创建密码到文件的方法。但我无法搜索用户输入的特定密码。我在程序部分的主要目标是创建一个“if”语句,检查计算机是否找到了特定的密码

我不熟悉使用find语句,在过去的几天里,我浏览了很多问题和示例,但都没有用——我无法理解它将如何与我的代码一起工作

def Lock_Screen():
    print("Welcome")

    print("Log in(1) or Sign up(2)") 
    User_Choice = input("")

    if User_Choice == "1":
        print("Input your three-digit code:")
        UserInput = input(str(""))
        x = open("PasswordList.txt" , "r")
        x.find(Userinput)
        if x.find(UserInput):
            Start_Screen()



    elif User_Choice == "2":        
        Password = random.randint(100,999)
        f = open("PasswordList.txt" , "a+")
        x = open("PasswordList.txt" , "r")
        PasswordList = x.read()
        while Password == PasswordList:                 
            Password = random.randint(100,999)  



        print("This is your password:    " , Password)
        f = open("PasswordList.txt" , "a+")                      
        f.write(str(Password))            
        f.close()
        Lock_Screen()


    else:                                    
        print("That was not a valid answer") 
        Lock_Screen()       

Lock_Screen()

.find语句应该查找变量,但它只返回一条错误消息


Tags: 用户txtlock密码inputifpassword语句
1条回答
网友
1楼 · 发布于 2024-09-27 09:29:23

打开的文件没有名为find的方法。你好像在找

Userinput = input("Input your three-digit code: ")

with open(filename) as x:
    for line in x:
        if line.rstrip('\n') == Userinput:
            print("password correct")
            break
    else:
        print("password not found")

完成后,with上下文管理器负责关闭打开的文件句柄

相关问题 更多 >

    热门问题