gpt4 book ai didi

Python if 语句说 2 个字符串不相同

转载 作者:太空宇宙 更新时间:2023-11-04 10:31:38 24 4
gpt4 key购买 nike

import time
#settings file (file with username/password)
settings = "settings.txt"
#set true to skip login
login = False
while not login:

print("""
/------------------------------\\\n
/ \\\n
/ Please Enter Your Username \\\n
/ \\\n
/ \\\n
/------------------------------\\\n
""")
username = input()
print("""\n\n\n\n\n\n\n\n
/------------------------------\\\n
/ \\\n
/ Please Enter Your Password \\\n
/ \\\n
/ \\\n
/------------------------------\\\n
""")
password = input()
#open file in read and write mode
file = open(settings, "r+")
for line in file:
if "username: " in line:
line = line.replace("username: ", "")
if username == line:
print("test")
file.close()

当我运行它并全部填写时,它不会打印测试我正在输入用户名,但它不会工作。它到达 if username == 行:
打印(“测试”)
并且不打印测试

脚本/应用程序是我学校的作业

这是设置文件

username: username
password: password

最佳答案

首先您需要检查用户名是否在txt 文件中!那么你在行尾有 \n 或使用 line.strip() 删除'\n'!所以将 if 语句更改为:

if username+'\n' == line:  #if user name is txt use 'username' 

if username == line.strip():

关于Python if 语句说 2 个字符串不相同,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26104533/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com