Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/299.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python基本if语句_Python - Fatal编程技术网

Python基本if语句

Python基本if语句,python,Python,大家好,下面的代码我只想在第一个条件满足时执行,所以请帮助我 print('welcome') username = input() if(username == "kevin"): print("Welcome"+username) else: print("bye") password = input() if(password == "asdfg"): print("Acess Granted") else: print("You Are not Kevin") 如果我答对了,您

大家好,下面的代码我只想在第一个条件满足时执行,所以请帮助我

print('welcome')
username = input()
if(username == "kevin"):
 print("Welcome"+username)
else:
 print("bye")
password = input()
if(password == "asdfg"):
 print("Acess Granted")
else:
 print("You Are not Kevin")

如果我答对了,您希望在第一条If语句的“else”块退出/终止脚本

有两种方法-第一种是使用sys.exit():

第二个是将第二个if放在第一个if的else块下:

...
else:
    password = input()
    if password=="abcdef":
        #do something here
    else:
        print("you're not Kevin")
另外,我看到您在if语句后面使用了括号(
if(condition):
)。就我而言,这对于Python3.x来说是不必要的


请参阅:

您能解释一下您想做什么吗?我想您想把
password=input()中的部分
在第一个if条件下……只要将所有内容置于满足该条件时希望发生的条件下……您没有提出合理的问题。我只是想显示如果第一个条件为真,则代码必须继续,如果为假,则应执行第二个代码。我清楚了吗?请在此帮助。请在此帮助Broomanks bro我会试试,很抱歉,当我使用第一种类型时,即使我输入kevin,它也不起作用。代码结束,请帮助bro。你的Python版本是什么?我使用的是Python 3.x broMake确保你只在真正想退出时才编写sys.exit()。不在if语句的第一部分。另外,尝试使用第二个版本。这更安全,也是更好的做法:)
...
else:
    password = input()
    if password=="abcdef":
        #do something here
    else:
        print("you're not Kevin")