Python 再次播放功能不工作

Python 再次播放功能不工作,python,Python,当输入n或no时,测验不会中断,它只会像输入y或yes时一样再次循环测验。此yes\u no函数的代码在底部我只需要代码方面的帮助谢谢 当输入n或no时,测验不会中断,它只会像输入y或yes时一样再次循环测验。此yes\u no函数的代码在底部我只需要代码方面的帮助谢谢 score = 0 def strchecker(question): valid=False while not valid: user_Name = input(question)

当输入n或no时,测验不会中断,它只会像输入y或yes时一样再次循环测验。此yes\u no函数的代码在底部我只需要代码方面的帮助谢谢 当输入n或no时,测验不会中断,它只会像输入y或yes时一样再次循环测验。此yes\u no函数的代码在底部我只需要代码方面的帮助谢谢

score = 0

def strchecker(question):
    valid=False
    while not valid:
        user_Name = input(question)
        if user_Name!="":
            valid=True
            return user_Name
        else:
            print("Please do not leave username blank")

print("*************Welcome to the Te Reo Maori Quiz***************\n"
       "You will be give a series of 6 questions,\nto answer you will enter an answer between 1 and 4.\n\nBest of Luck,and remember if you would like to quit the game just press enter :)\n")

user_Name = strchecker("Please enter your username:\n")

print("Hi", user_Name,"Here is your first question:\n")

keep_going=""
while keep_going=="":

#  List of questions in Quiz
    question_List = ["How do you write number 1 in Maori?\n1.Tekau 2.Tahi 3.Ono 4.Rua",
                      "What is does tahi + tahi = ?\n1.Rua 2.Rimu 3.Ono 4.Tahi",
                      "How do you write blue in Maori?\n1.Kakariki 2.Kikorangi 3.Whero 4.Ma",
                      "What two colours make blue?\n1.Ma + Whero 2.Kikorangi + Kowhai 3.Whero + Pararui 4.Ma + Mangu",
                      "Who was the god of the forest and birds?\n1.Ranginui 2.Paptuanuku 3.Tane-Mahuta 4.Tangaroa",
                      "Who were Tane Mahutas Parents?\n1.Tangaroa + Ranguinui 2.Punga + Ranganui 3.Tangaroa + Rongo 4.Papatunuku + Ranganui"]

    # List of Correct Answers
    correct_Answer = [2, 1, 2, 2, 3, 4]


    # If user enters anything that is not an integer between 1 and 4 it will be an invalid input 
    def intcheck(question, low, high):
        valid= False
        while not valid:
            error= "Whoops! Please enter an integer between {} and {}\n".format(low, high)
            try:
                response = int(input("Please enter your answer or press enter to quit\n"))

                if low <= response <= high: 
                    return response
                else:
                    print(error)
                    print()
            except ValueError:
                  print(error)


    # Get a question from the question list and print and loop one by one 
    for idx, question in enumerate(question_List):
        print(question)

        Answer = intcheck("Please enter in an answer or press enter to quit", 1,4)
        print()
    # Get answer and check if it is correct or incorrect by going to the list of correct answers 
        if Answer == correct_Answer[idx]:
                print("Well Done, your answer was correct\n")
                score +=1
        else:
             print("Hard Luck, your answer was incorrect\n")

    if score <4:
        print("************************************************************")
        print("You got",score,"out of 6.\n\nYou should get more than 3/6, try the quiz again to improve your knowledge.\n\n")
    elif score >4 or score<7:
        print("*************************************************************")
        print("You got",score,"out of 6.\n\nNice job! Your study payed off!\n\n")

    def strchecker(question):
        valid=False
        while not valid:
            response=input(question)
            if response.lower()=="yes" or response.lower()=="y":
                response="yes"
                valid=True
                return response
                keep_going

            elif response.lower()=="no" or response.lower()=="n":
                response="no"
                valid=True
                return response
                print(" Thank you for playing. You have chose to quit the game")
                break
            else:
                print("Invalid input, please enter yes or no\n\n")

    yes_No = strchecker("Would you like to play again\n")

您必须知道,当函数到达return语句时,它将结束。例如,如果您在返回语句之后打印某些内容,它将永远不会被打印。这个脚本有很多问题。首先,我觉得自己是第一个strchecker func。没有正当理由存在,因为它只正确使用了一次。第二,在满足条件之前,您不需要分配一个变量来让循环无休止地运行。这是您的脚本中最缺少的

简介:While-True语法:

while True:
    do stuff
    if stuff == stuff i wanted:
        break
    else:
        pass
现在这里else-pass意味着只需转到循环的顶部并继续,这通常是隐含的,您不需要在while-True循环中显式地编写else-pass部分

这里是您的脚本的一个修改版本,它使用了这个概念,我已经替换了大多数我认为效率低下的东西,并改变了代码的动态性,但我相信本质仍然存在

注:变量c是一个计数器,变量inp代表输入并存储最终的是/否输入,分数是计算出来的,但只是打印出来,而不是str操作和格式化我相信你自己可以做到,为了更容易遵循PEP8标准,我们进行了一些更改,例如:将问题列表拆分为问题列表和选项列表

#!/usr/bin/env python


def checker(query):
    while True:
        user_in = input(query)
        if user_in == "":
            end = True
            return end
        elif user_in in ['1', '2', '3', '4', '']:
            return user_in
            break
        else:
            print("Invalid input")


def main():
    score = 0
    print("*************Welcome to the Te Reo Maori Quiz***************\n",
          "You will be give a series of 6 questions,\n",
          "to answer you will enter an answer between 1 and 4.\n",
          "\nBest of Luck,and remember if you would like to quit the game ",
          "just press enter :)\n")

    while True:
        user_name = input("Please enter your username:\n")
        if user_name == "":
            print("Please do not leave username blank")
        else:
            print("Hi {}, here is your first question:\n".format(user_name))
            break
    question_List = ["How do you write number 1 in Maori?",
                     "What is does tahi + tahi = ?",
                     "How do you write blue in Maori?",
                     "What two colours make blue?",
                     "Who was the god of the forest and birds?",
                     "Who were Tane Mahutas Parents?"]

    options_list = ["1.Tekau 2.Tahi 3.Ono 4.Rua",
                    "1.Rua 2.Rimu 3.Ono 4.Tahi",
                    "1.Kakariki 2.Kikorangi 3.Whero 4.Ma",
                    "1.Ma + Whero 2.Kikorangi + Kowhai 3.Whero + Pararui 4.Ma + Mangu",
                    "1.Ranginui 2.Paptuanuku 3.Tane-Mahuta 4.Tangaroa",
                    "1.Tangaroa + Ranguinui 2.Punga + Ranganui 3.Tangaroa + Rongo 4.Papatunuku + Ranganui"]
    correct_Answer = [2, 1, 2, 2, 3, 4]

    c = 0
    while c <= 5:
        print("{}\n{}".format(question_List[c], options_list[c]))
        ans = checker("Please enter in an answer or press enter to quit\n")
        if ans is True:
            break
        else:
            if ans == str(correct_Answer[c]):
                print("Well Done, your answer was correct\n")
                score += 1
                c += 1
            else:
                print("Hard Luck, your answer was incorrect\n")
                c += 1
    print(score)


while True:
    main()
    while True:
        inp = input("Would you like to play again\n")
        inp = inp.lower()
        if inp in ['yes', 'no']:
            break
        else:
            print('Invalid Input')
    if inp == 'no':
        break
    elif inp == 'yes':
        pass

难道你没有意识到返回后的代码将不会被执行吗?不,对不起,我才12岁,只是在学习不要太在意注释,我们都必须从某个地方开始。return退出当前函数,因此该函数中返回后的代码将被忽略。例如:返回响应;打印谢谢。。。;中断-忽略打印和中断。还有:回复;继续,继续是没有用的,原因有两个,一个是它在返回之后,另一个是这是一个字符串,你没有将它设置为任何值。不要评论不起作用-这是愚蠢的用户说的,不是像你和我这样的it从业者。说到底发生了什么,你必须帮助我们帮助你。好吧,那么我该怎么做,不工作意味着当我输入no时,代码会再次循环,就像我在顶部解释的那样?对不起?那我该怎么办呢?谢谢你:你已经选择退出游戏声明,但没有破坏代码。请帮助,它工作只是它不工作,但打印声明工作很酷。尽管这是一件很小的事情,尝试编写更干净的代码。
#!/usr/bin/env python


def checker(query):
    while True:
        user_in = input(query)
        if user_in == "":
            end = True
            return end
        elif user_in in ['1', '2', '3', '4', '']:
            return user_in
            break
        else:
            print("Invalid input")


def main():
    score = 0
    print("*************Welcome to the Te Reo Maori Quiz***************\n",
          "You will be give a series of 6 questions,\n",
          "to answer you will enter an answer between 1 and 4.\n",
          "\nBest of Luck,and remember if you would like to quit the game ",
          "just press enter :)\n")

    while True:
        user_name = input("Please enter your username:\n")
        if user_name == "":
            print("Please do not leave username blank")
        else:
            print("Hi {}, here is your first question:\n".format(user_name))
            break
    question_List = ["How do you write number 1 in Maori?",
                     "What is does tahi + tahi = ?",
                     "How do you write blue in Maori?",
                     "What two colours make blue?",
                     "Who was the god of the forest and birds?",
                     "Who were Tane Mahutas Parents?"]

    options_list = ["1.Tekau 2.Tahi 3.Ono 4.Rua",
                    "1.Rua 2.Rimu 3.Ono 4.Tahi",
                    "1.Kakariki 2.Kikorangi 3.Whero 4.Ma",
                    "1.Ma + Whero 2.Kikorangi + Kowhai 3.Whero + Pararui 4.Ma + Mangu",
                    "1.Ranginui 2.Paptuanuku 3.Tane-Mahuta 4.Tangaroa",
                    "1.Tangaroa + Ranguinui 2.Punga + Ranganui 3.Tangaroa + Rongo 4.Papatunuku + Ranganui"]
    correct_Answer = [2, 1, 2, 2, 3, 4]

    c = 0
    while c <= 5:
        print("{}\n{}".format(question_List[c], options_list[c]))
        ans = checker("Please enter in an answer or press enter to quit\n")
        if ans is True:
            break
        else:
            if ans == str(correct_Answer[c]):
                print("Well Done, your answer was correct\n")
                score += 1
                c += 1
            else:
                print("Hard Luck, your answer was incorrect\n")
                c += 1
    print(score)


while True:
    main()
    while True:
        inp = input("Would you like to play again\n")
        inp = inp.lower()
        if inp in ['yes', 'no']:
            break
        else:
            print('Invalid Input')
    if inp == 'no':
        break
    elif inp == 'yes':
        pass