基本上,我想我的计划,让输入,这已经完成,但我想它不是让某个朋友,也只有这个朋友,不要使用该程序。我已经改变了我的朋友的实际名称为“朋友的名字”原因很明显。现在,当我输入他的名字它打印Unauthorized user detected
,并提示我重新输入,这是我想要的。
问题进来时,我再次输入相同的朋友的名字。我想程序打印,它再次是一个未经授权的用户和程序将终止。但我只能让程序的第二个名称项之后终止无论做什么,或者我可以缩进quit()
命令4个空间和程序进行跳过quit命令和运行程序的其余部分。
while True:
user_input == ("Friends Name")
print("Unauthorized user detected.")
input("Please enter a new name: ")
if input != "Friends Name":
break
else:
print("Unauthorized user detected, the program will now terminate.")
quit()
做在一个while循环迭代计数的一个好方法是使用计数的你的朋友的名字已被输入次数的变量。此外quit()
结束循环,删除。
counter = 0
while True:
name = input("Please enter you name")
if name == "Friends Name":
counter += 1
if counter == 2:
break
# or exit() if you want to program to quit in spite of code present later