Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/334.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语句为false,如何不运行其余的If语句?_Python_Python 3.x_If Statement_Nested - Fatal编程技术网

Python 如果一条If语句为false,如何不运行其余的If语句?

Python 如果一条If语句为false,如何不运行其余的If语句?,python,python-3.x,if-statement,nested,Python,Python 3.x,If Statement,Nested,对于以下程序,如果对任何问题的回答导致用户没有投票资格,那么我如何让程序立即这样说,而不询问剩余的问题 def main(): print("This program determines if a user is eligible to vote in the US\n") q1 = str(input("Are you a US citizen? y/n: ")) q2 = int(input("What is yo

对于以下程序,如果对任何问题的回答导致用户没有投票资格,那么我如何让程序立即这样说,而不询问剩余的问题

def main():
    print("This program determines if a user is eligible to vote in the US\n")

    q1 = str(input("Are you a US citizen? y/n: "))
    q2 = int(input("What is your age?: "))
    q3 = str(input("Do you meet your state's residency requirement? y/n: "))

    if q1 == "n":
        print("\nNot eligible to vote.")
    elif q2 < 18:
        print("\nNot eligible to vote.")
    elif q3 == "n":
        print("\nNot eligible to vote.")
    else:
        q1 == "y"
        q2 >= 18
        q3 == "y"
        print("\nYou are eligible to vote!")
main()
def main():
打印(“此程序确定用户是否有资格在美国投票\n”)
q1=str(输入(“您是美国公民吗?是/否:”)
q2=int(输入(“您的年龄:”)
q3=str(输入(“您符合您所在州的居住要求吗?是/否:”)
如果q1==“n”:
打印(“\n没有资格投票。”)
elif q2<18:
打印(“\n没有资格投票。”)
elif q3==“n”:
打印(“\n没有资格投票。”)
其他:
q1==“y”
q2>=18
q3==“y”
打印(“\n您有资格投票!”)
main()
使用嵌套的“if-else”语句,当其中一个问题出错时退出。像这样:

def main():
print("This program determines if a user is eligible to vote in the US\n")

q1 = str(input("Are you a US citizen? y/n: "))
if q1 == 'y':
    q2 = int(input('What is your age?:  '))
    if q2 > 18:
        q3 = str(input('Do you meet your states residency requirement? y/n:  '))
        if q3 == 'y':
            print("\nYou are eligible to vote!")
        else:
            print("\nNot eligible to vote.")
            exit()
    else:
        print("\nNot eligible to vote.")
        exit()
else:
    print("\nNot eligible to vote.")
    exit()
main()

在每次
input
语句之后,您必须检查用户的输入。每次都可以使用if-else语句。如果答案错误,请打印一些内容,然后使用
return
,而
main()
函数中的剩余代码将不会执行。其余的问题将不会被询问。

如果您以后不再需要问题的结果,您可以将
输入
放入
If
条件中,并将其与
链接。这样,第二个
输入
不会再次询问第一个输入是否已经决定了条件的结果,第三个输入也是如此

if (input("Are you a US citizen? y/n: ") == "y" and
        int(input("What is your age?: ")) >= 18 and
        input("Do you meet your state's residency requirement? y/n: ") == "y"):
    print("\nYou are eligible to vote!")
else:
    print("\nNot eligible to vote.")

您还可以将其与
(…)
组合,以获得更复杂的条件,尽管在某些情况下,使用嵌套的
if/else
结构可能会更具可读性。

询问问题1,检查其是否有效,
如果无效,则返回
。然后问问题2,冲洗并重复。请修正你的压痕。嗯。。。这不是重点。我只是在演示fizzy需要的代码。