我是python和stackoverflow的新手,如果我不太了解,请道歉。我最近选择了Python,并开始使用它。我一直试图登录管理员的东西,但我似乎得到一个错误。我尝试过使用for语句,但我似乎得到了错误:TypeError:' str'对象不可调用 这是我的代码......
username = input("Please enter your username: ")
for name in username (3):
if username == 'admin':
break
else:
print("You've given incorrect credentials thrice.")
import sys
sys.exit("Exiting...")
while True:
password = input("Please enter your password: ")
if password == 'KhS9':
break
print("Welcome admin!")
对此有何帮助?
修改的
道歉,如果我不清楚,我试图尝试制作你需要输入的东西.for循环用于尝试得到句子"请输入你的用户名"重复3次,然后显示句子"你已经给出了3次不正确的凭据"如果输入与我设置的输入不匹配(我将其设置为' admin')
答案 0 :(得分:1)
嗯,那是什么?
DateTimeFormatter
java.sql.Date sqlDate = java.sql.Date.valueOf( localDate );
是一个字符串,而不是函数。
如果您想要三首字母,请执行
username = username [3]
脚本:
for name in username (3):
答案 1 :(得分:0)
我想你想要
for i in range(3):
#some code
这意味着放置一些代码的代码将运行三次。但是,你的else语句将评估为true,从而在第一次运行时打破循环。这意味着你需要以某种方式修改它 - 添加一个计数器变量。
在输入正确的密码之前,请考虑运行while True
循环。不安全?
只需要语法上的东西 - 即使它意味着你不使用它,也可以在模块顶部(页面)导入一些东西。
答案 2 :(得分:0)
以下是您如何正确实施该想法的示例: admin_name ='abc'#默认用户名(未加密=不安全)。 admin_pass ='cde'#默认密码(未加密=不安全)。
max_attempts = 3 # Maximum number of times a user can try.
attempt = 0 # Initial condition when the programme is launched.
# Hereby, we'll take invalid attempts as an error, but instead of breaking the
# programme, we will later handle (via "exceptions" the error, and issue
# additional guidance.
try:
# This is an event loop. It will run for as long as the condition set is true.
while attempt < max_attempts:
uname = input('Username: ') # User is directed to enter the username.
password = input('pass: ') # User is directed to enter the password.
# Values entered by the user are validated.
if uname == admin_name and password == admin_pass:
# If the entries are valid, the event loop is broken, and anything
# written afterwards ( after the exceptions) will now be executed,
# unless directed otherwise (e.g. a certain function is called)
# before the "break".
print('Welcome Admin')
break
else:
# If the entries are invalid, but the maximum number of attempts
# are not reached, one is added to the attempts, followed by a
# warning. "continue" means that the rest of the loop is no longer
# executed, and the event loop automatically moves onto the
# forthcoming loop immediately. This is not essential here, but I
# put it in case something else is added to the event loop.
attempt += 1
if attempt == max_attempts:
# if the maximum number of attempts are reached in this loop, a
# warning is displayed, and the next cycle would not be run
# automatically. It is best to terminate the programme afterwards,
# so the following code wouldn't run.
raise RuntimeError("\nYou've reached the maximum number of attempts allowed.")
else:
print('Wrong credentials.\n Try again or press <ctrl+c> to exit.\n')
continue
except KeyboardInterrupt:
# "KeyboardInterrupt" is a special exception raised only when the
# programme is broken out of using <ctrl+c>. In that case, this piece of
# code is run.
print('Terminated by the user.\nGood-bye.')
except RuntimeError as e:
# This is handling of the errors raised above. The value of "e" is equal
# the value given to the relevant function (RuntimeError in this case)
# when it was raised.
print(e)
不用说,这是一种存储管理员密码的不安全方式。也许利用Python的crypt
模块来加密密码?