当我输入错误的密码是打印下一行,所以,如果密码不正确,如何阻止用户访问下一行


input = input("Enter your coden ") 
if input == "MB69": 

print ('Mayank bansal') 

else:
print ("code " + input + " is not correct")
print ("welcome")

您可以使用sys.exit()。在您的代码中:

import sys
inp = input("Enter your coden ")
if inp == "MB69":
print('Mayank bansal') 
else: 
print ("code " + inp + " is not correct")
sys.exit(0)
print("welcome")

参考:https://docs.python.org/3/library/sys.html#sys.exit

您可以使用sys.exit(0)。或者,您可以定义一个函数。

import sys
input = input("Enter your coden ") 
if input == "MB69": 
print ('Mayank bansal') 
else:
print ("code " + input + " is not correct")
sys.exit(0)
print ("welcome")

通过定义功能

def success():
print ("welcome")
input = input("Enter your coden ") 
if input == "MB69":    
print ('Mayank bansal') 
success()
else:
print ("code " + input + " is not correct")

试试这个-

def ask():
password = input("Enter your coden ") 
if password == "MB69": 

print ('Mayank bansal') 

else:
print ("code " + input + " is not correct")
return

print ("welcome")

你可以让它变得更简单-

def ask():
password = input("Enter your coden ") 
if password != "MB69": 
print ("code " + input + " is not correct")
return
print ('Mayank bansal')
print ("welcome")

如果你使用第二种方法,该函数将返回并停止进一步运行,除非你给出正确的密码

相关内容

最新更新