如何重复一段代码而不重复所有代码?蟒蛇

时间:2016-03-20 00:35:44

标签: python

干草我正在为一个学校项目做一些代码,并想知道如何让它变小(这就是我所拥有的)......

PersonName = input("Enter Name To Prove Your Identity: ")
Answer = input("Name Inputed Is " + PersonName + " Is This Correct(enter yes or no): ")
if Answer.lower() == "yes":
    print("You May Continue " + PersonName + " ")
if Answer.lower() == "no".lower():
    print("Please Reset Your Name")
    PersonName = input("Enter Name To Prove Your Identity: ")
    Answer = input("Name Inputed Is " + PersonName + " Is This Correct(enter yes or no): ")
    if Answer.lower() == "yes".lower():
        print("You May Continue " + PersonName + " ")
    if Answer.lower() == "no".lower():
        print("Please Reset Your Name")
        PersonName = input("Enter Name To Prove Your Identity: ")
        Answer = input("Name Inputed Is " + PersonName + " Is This Correct(enter yes or no): ")
        if Answer.lower() == "yes".lower():
            print("You May Continue " + PersonName + " ")
        if Answer.lower() == "no".lower():
            print("Please Reset Your Name")
            PersonName = input("Enter Name To Prove Your Identity: ")
            Answer = input("Name Inputed Is " + PersonName + " Is This Correct(enter yes or no): ")
            if Answer.lower() == "yes".lower():
                print("You May Continue " + PersonName + " ")
            if Answer.lower() == "no".lower():
                print("Please Reset Your Name")
                PersonName = input("Enter Name To Prove Your Identity: ")
                Answer = input("Name Inputed Is " + PersonName + " Is This Correct(enter yes or no): ")
                if Answer.lower() == "yes".lower():
                    print("You May Continue " + PersonName + " ")
                if Answer.lower() == "no".lower():
                    print("Please Reset Your Name")
                    PersonName = input("Enter Name To Prove Your Identity: ")
                    Answer = input("Name Inputed Is " + PersonName + " Is This Correct(enter yes or no): ")
                    if Answer.lower() == "yes".lower():
                        print("You May Continue " + PersonName + " ")
                    if Answer.lower() == "no".lower():
                        print(" ")
                        print("SORRY CAN'T START LAUNCH BECAUSE YOU CAN'T PROVE YOUR IDENTITY")

你可以告诉我这是分配代码,如果你想让我在评论中提出我的完整代码,我需要为其他部分做这个,所以我需要把它缩小。我是PYTHON的新手,所以如果有人可以给出一个例子/告诉我怎么做以使这个代码更小,我将非常感激

谢谢;)

2 个答案:

答案 0 :(得分:1)

尝试这一点,也可以使用raw_input(),因为你正在使用字符串

def get_input_name():
    PersonName = raw_input("Enter Name To Prove Your Identity: ")
    Answer = raw_input("Name Inputed Is " + PersonName + " Is This Correct(enter yes or no): ")
    if Answer.lower() == "yes":
        print("You May Continue " + PersonName + " ")
    else:
        get_input_name()


def main():
    get_input_name()

if __name__ == '__main__':
    main()

答案 1 :(得分:0)

输入正确后,使用loopbreak离开:

while True:
    PersonName = input("Enter Name To Prove Your Identity: ")
    Answer = input("Name Inputed Is " + PersonName + " Is This Correct(enter yes or no): ")
    if Answer.lower() == "yes":
        print("You May Continue " + PersonName + " ")
        break  # leave loop
    else:
        print("Please Reset Your Name")

您还可以为固定次数添加一些计数器。