如何在Python中使用带嵌套if语句的while循环

如何在Python中使用带嵌套if语句的while循环,python,loops,while-loop,Python,Loops,While Loop,在从我的代码中得到了一堆不想要的输出后,我意识到我可能对for和while循环没有清晰的理解。我的意图是在while或for循环中“嵌套”一个if块,当用户输入一个不期望的答案时,它不会退出,而是简单地返回(而不是无限地)直到正确或至少期望的输入被接收。请帮忙,可能的话请解释一下。多谢各位 employment_history = str(input("Have you ever been employed? Y/N: " ).title()) if employment_history ==

在从我的代码中得到了一堆不想要的输出后,我意识到我可能对for和while循环没有清晰的理解。我的意图是在while或for循环中“嵌套”一个if块,当用户输入一个不期望的答案时,它不会退出,而是简单地返回(而不是无限地)直到正确或至少期望的输入被接收。请帮忙,可能的话请解释一下。多谢各位

employment_history = str(input("Have you ever been employed? Y/N: " ).title())
if employment_history == "Y":

    comp_name = str(input("What was the name of the company you worked for? ").title())
    ref_1 = str(input("Who was your reference at "+ comp_name +"? ").title())
    ref_1_num = input("What was "+ ref_1 + "'s number? ")
    ref_1_number = ref_1_num[0:3]+'-'+ ref_1_num[3:6]+'-'+ ref_1_num[6:10]
    print("The company you worked for was "+comp_name+", "+ ref_1 +" was your reference ", "their number was "+ ref_1_number +".")


elif employment_history == "N":

    print("No employment record entered.")

while employment_history != "Y" and "N":
    print("Please enter Y for 'YES' and N for 'NO'.")
    return

您可以将if语句放入循环中,以便每次循环时它都能到达它们

while True:
    print("Please enter Y for 'YES' and N for 'NO'.")
    employment_history = str(input("Have you ever been employed? Y/N: " ).title())
    if employment_history == "Y":

        comp_name = str(input("What was the name of the company you worked for? ").title())
        ref_1 = str(input("Who was your reference at "+ comp_name +"? ").title())
        ref_1_num = input("What was "+ ref_1 + "'s number? ")
        ref_1_number = ref_1_num[0:3]+'-'+ ref_1_num[3:6]+'-'+ ref_1_num[6:10]
        print("The company you worked for was "+comp_name+", "+ ref_1 +" was your reference ", "their number was "+ ref_1_number +".")
        break

    elif employment_history == "N":
        print("No employment record entered.")

这段代码是一个无限循环,但是,您可以使用if语句检查是否满足要求。如果他们是,那么它将遍历代码,并在语句末尾打破循环,这样它就不会再问了。

你相信什么“Y”和“N”:可以吗?考虑到
“N”
将始终是
“N”
,除此之外什么都不是“Y”和“N”是测试变量多个值的错误方法。查看此问题,因为用户应给出Y或N作为答案,其他任何内容都应打印“提示用户输入Y或N的消息”。我意识到我可能没有按正确的顺序放置While语句。问题是
和“N”
被视为一个完全独立的条件,非空字符串始终为true,因此
语句的右半部分将始终为true。“请帮助并可能解释。”你不能解释你对OP的代码所做的更改吗?
employment_history = None 

while employment_history != "N":

    employment_history = str(raw_input("Have you ever been employed? Y/N: " ))

    if employment_history == "Y":
        comp_name = str(raw_input("What was the name of the company you worked for? ").title())
        ref_1 = str(raw_input("Who was your reference at "+ comp_name +"? ").title())
        ref_1_num = raw_input("What was "+ ref_1 + "'s number? ")
        ref_1_number = ref_1_num[0:3]+'-'+ ref_1_num[3:6]+'-'+ ref_1_num[6:10]
        print("The company you worked for was "+comp_name+", "+ ref_1 +" was your reference ", "their number was "+ ref_1_number +".")

    elif employment_history == "N":
        print("No employment record entered.")