Python while循环问题-意外重复

问题描述

我正在尝试编写代码以询问一系列问题,如果用户回答“否”,则while循环中断,并显示一条语句。我的代码的这一部分工作。但是,如果用户对第一个问题的回答为“是”,则只会不断重复询问相同的问题。有什么想法吗?谢谢!

#The first set of questions is to screen whether or not the assignment should be further graded as if 
#they Failed to complete any of the four following tasks,they will receive a "0" for their grade.
answer = "Yes"
while answer == "Yes":
    answer = input("Is the assignment a single uncompressed .py file?  Yes or No: ")
    if answer != "Yes":
        break
        answer = input("Did the student include both their name and date?  Yes or No: ")
    if answer != "Yes":
        break
        honor_statement = input("Did the student include the honor statement,'I have not given or 
received any unauthorized assistance on this assignment.'?  Yes or No: ")
    if answer != "Yes":
        break
        youtube_link = input("Did the student include a link to an unlisted 3 minute YouTube video 
presenting the code and answering the assigned questions?  Yes or No: ")
    if answer != "Yes":
        break
print("The student is awarded a grade of '0' because he or she Failed to complete one or more of the 
requisite assignment items in full.")

解决方法

您不需要循环。您不想重复问同样的问题,只想问四个独立的问题。因此,请使用嵌套的if语句。

为每个问题分配一个不同的变量,然后在最后测试所有变量。

uncompressed = input("Is the assignment a single uncompressed .py file?  Yes or No: ")
if uncompressed == "Yes":
    namedate = input("Did the student include both their name and date?  Yes or No: ")
    if namedate == "Yes":
        honor_statement = input("Did the student include the honor statement,'I have not given or received any unauthorized assistance on this assignment.'?  Yes or No: ")
        if honor_statement == "Yes":
            youtube_link = input("Did the student include a link to an unlisted 3 minute YouTube video presenting the code and answering the assigned questions?  Yes or No: ")
            if youtube_link == "Yes":
if uncompressed != "Yes" or namedate != "Yes" or honor_statement != "Yes" or youtube_link != "Yes": 
    print("The student is awarded a grade of '0' because he or she failed to complete one or more of the requisite assignment items in full.")