每当我导入此脚本时,它都会在不被调用的情况下运行,并且为了避免需要使用 if __name__ =='__main __':,我很困惑如何在此代码中使用它 如果有人可以帮助我,那就太好了
import pyperclip
passwords = {'Facebook': 'xxxx', 'Instagram': 'xxxxxx', 'Youtube': 'xxxxxxx'}
tries = 0
tries_remaining = 3
pass_len = len(passwords)
def ask():
mast_pass = 'HailHydra'
ask_mast_pass = input('Please enter the master password: ')
if ask_mast_pass == mast_pass:
print(f"{pass_len} found.")
for x in passwords.keys():
print(x)
which_pass = input('Type the name of the password title you are
looking for: ')
paswd = passwords[which_pass]
pyperclip.copy(paswd)
print('Password Copied to clipboard.')
exit()
else:
print('Incorrect Password, Please Try again')
while tries < tries_remaining:
tries += 1
ask()
print(f"{3 - tries} tries remaining")
print('***Do not try to breach the system***')
答案 0 :(得分:2)
将while循环置于__name__
检查下。这就是导入该脚本时将运行的内容。
import pyperclip
passwords = {'Facebook': 'xxxx', 'Instagram': 'xxxxxx', 'Youtube': 'xxxxxxx'}
tries = 0
tries_remaining = 3
pass_len = len(passwords)
def ask():
mast_pass = 'HailHydra'
ask_mast_pass = input('Please enter the master password: ')
if ask_mast_pass == mast_pass:
print(f"{pass_len} found.")
for x in passwords.keys():
print(x)
which_pass = input('Type the name of the password title you are
looking for: ')
paswd = passwords[which_pass]
pyperclip.copy(paswd)
print('Password Copied to clipboard.')
exit()
else:
print('Incorrect Password, Please Try again')
if __name__ == '__main__':
while tries < tries_remaining:
tries += 1
ask()
print(f"{3 - tries} tries remaining")
print('***Do not try to breach the system***')