【发布时间】:2019-10-13 23:20:42
【问题描述】:
如何修复此 Python 代码中的变量赋值?所以,我有这个 python 代码:
with open('save.data') as fp:
save_data = [line.split(' = ') for line in fp.read().splitlines()]
with open('brute.txt') as fp:
brute = fp.read().splitlines()
for username, password in save_data:
if username in brute:
break
else:
print("didn't find the username")
好的,简单解释一下; save.data 是一个包含批处理文件游戏变量(例如用户名、hp 等)的文件,brute.txt 是一个包含“随机”字符串的文件(就像在用于暴力破解的单词列表中看到的那样) )。
save.data:
username1 = PlayerName
password1 = PlayerPass
hp = 100
正如我之前所说,这是一个批处理文件游戏,所以不需要引用字符串。
brute.txt:
username
username1
password
password1
health
hp
因此,当 Python 代码执行时,它会加载两个文件的内容并将它们保存到一个列表中,然后遍历用户名和密码“蛮横”它们,直到它们与 brute.txt 上的内容匹配,然后它们自己分配自动地。但是,问题在于分配,当我尝试 print 他们(变量)时,会发生这种情况:
## We did all the previous code
...
>>> print(save_data)
[['username', 'PlayerName'], ['password', 'PlayerPass'], ['health', '100']]
>>> print("Your username is: " + username)
username
>> print("Your password is: " + password)
PlayerName
>> print("Your health is: " + hp)
NameError: name 'hp' is not defined
那么,关于如何解决分配冲突的任何想法?有不明白的地方欢迎评论,我会一一解答。
【问题讨论】:
标签: python-3.x list for-loop variable-assignment brute-force