再次播放功能不工作

2024-04-19 22:00:37 发布

您现在位置: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)
        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")

Tags: orandofthetonoanswerif
2条回答
def strchecker(question):
        valid=False
        while not valid:
            response=input(question)
            if response.lower()=="yes" or response.lower()=="y":
                response="yes"
                valid=True # will not loop, useless here
                keep_going
                return response # return yes
            elif response.lower()=="no" or response.lower()=="n":
                response="no"
                valid=True # will not loop, useless here
                print(" Thank you for playing. You have chose to quit the game")
                return response #return no
            else:
                print("Invalid input, please enter yes or no\n\n")

您必须知道,当函数到达return语句时,它就结束了。例如,如果在return语句之后打印某个内容,它将永远不会被打印

好吧。这有很多问题脚本。首先,我觉得自己是第一个strchecker()函数。没有正当的理由存在,因为它只被正确地使用过一次。第二,您不需要仅仅为了使循环无限地进行而分配一个变量,直到满足一个条件(这是您的脚本中最缺少的)。你知道吗

介绍:While True语法:

while True:
    do stuff
    if stuff == stuff i wanted:
        break
    else:
        pass

这里的else-pass意味着只要转到循环的顶部并继续,这通常是隐含的,您不需要在while-True循环中显式地编写else-pass部分。你知道吗

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

注:variablec是一个计数器,variableinp代表输入并存储最终的yes/no输入,分数是计算出来的,只是简单地打印出来,而不是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

相关问题 更多 >