再次播放功能不起作用

时间:2018-06-29 09:25:03

标签: python

输入n或no时测验不会中断,它将像输入y或yes一样再次循环测验。(此yes_No函数的代码在底部)我只需要帮助与代码谢谢 输入n或no时测验不会中断,它将像输入y或yes一样再次循环测验。(此yes_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")

2 个答案:

答案 0 :(得分:1)

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语句之后打印某些内容,则永远不会打印

答案 1 :(得分:0)

好的。这个脚本有很多问题。首先,我感觉像第一个strchecker()函数。没有合理的理由存在,因为仅正确使用了一次。其次,您不需要分配变量就可以使循环无限循环直到满足条件(这是脚本中最缺乏的)。

简介:While True语法:

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

现在这里的其他传递意味着只需要循环到顶部并继续,这通常是隐含的,您不需要在True循环中显式地编写其他传递部分。

这是使用此概念的脚本的修改版本,我替换了我认为效率低下的大多数内容并更改了代码的动态,但我相信本质仍然存在。

注意:变量 c 是一个计数器,变量 inp 代表输入并存储最终的是/否输入,仅计算分数但只打印输出而不是str操作和格式设置(我敢肯定您可以自己做),为了使遵循PEP8标准更容易(例如:将questionsList分为questionsList和option_list),存在一些更改。

#!/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