【发布时间】:2017-02-22 18:18:44
【问题描述】:
我有这个代码:
while True:
uName = []
uPassword = []
maxLengthList = 1
maxPassList = 1
acceptEnt = ["YES", "yes", "Y", "y"]
denyEnt = ["NO", "no", "N", "y"]
while len(uName) < maxLengthList:
item = raw_input("Create a username: ")
uName.append(item)
checkU = raw_input("Is %s correct? " % uName)
if checkU in acceptEnt:
while len(uPassword) < maxPassList:
pw = raw_input("Create a password: ")
uPassword.append(pw)
checkP = raw_input("Is %s correct? " % uPassword)
if checkP in acceptEnt:
print "%s logging in..." % uName
else:
if checkU in denyEnt or checkP in denyEnt:
print "Error"
break
应该让用户创建一个用户名和密码,然后传递给这个:
done = True
while done == True:
sys.stdout.write('\rLoading')
time.sleep(0.11)
sys.stdout.write('\rLoading. ')
time.sleep(1)
sys.stdout.write('\rLoading.. ')
time.sleep(2)
sys.stdout.write('\rLoading... ')
time.sleep(1)
sys.stdout.write('\rLoading.... ')
time.sleep(0.39)
sys.stdout.write('\rLoading..... ')
time.sleep(0.19)
sys.stdout.write('\rLoading...... ')
time.sleep(0.25)
done = False
sys.stdout.write('\rInitializing')
time.sleep(3)
sys.stdout.write('\rHello, %s' % uName)
time.sleep(1.5)
仅模拟加载操作。在用户输入用户名和密码之前,我需要它不传递给该代码块,但即使用户在询问 %s 是否正确时输入“否”,它也会传递给下一个代码。
我试图让它坚持创建帐户,直到输入真正的输入,当输入“否”时,然后将用户返回到之前的创建屏幕。我根本不希望他们能够绕过那部分,这是他们可以做的。
更新:
我最终将其更改为布尔值,虽然我打算稍微清理一下代码,但对我来说似乎更快。
entry = 真
当条目 == 真时:
uName = []
uPassword = []
maxLengthList = 1
maxPassList = 1
acceptEnt = ["YES", "yes", "Y", "y"]
denyEnt = ["NO", "no", "N", "y"]
while len(uName) < maxLengthList:
item = raw_input("Create a username: ")
uName.append(item)
checkU = raw_input("Is %s correct? " % uName)
if checkU in acceptEnt:
while len(uPassword) < maxPassList:
pw = raw_input("Create a password: ")
uPassword.append(pw)
checkP = raw_input("Is %s correct? " % uPassword)
if checkP in acceptEnt:
print "%s logging in..." % uName
entry = False
break
else:
print "pw wrong test"
else:
print "uname wrong test"
break
这似乎暂时有效,但我肯定会再次访问它,因为我会寻找此处提到的替代方案。
【问题讨论】:
-
严格来说不是重复的,但Asking the user for input until they give a valid response 可能会给你一些有用的想法。
标签: python python-2.7