随时退出测验功能-Python

时间:2018-06-30 12:14:01

标签: python

我试图制作一个可以在测验中随时输入“ q”时退出游戏的函数,但是它不起作用,它在intcheck函数下并且是strchecker,我还有另一个strchecker功能也许就是为什么它不起作用。我知道它需要打破一个循环,我只是python的初学者。

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 'q' to quit"))

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

            except ValueError:
                  print(error)

    def strchecker(question):
        valid=False
        while not valid:
            Quit = input(question)
            if user_Name!="q":
                valid=True
                return user_Name
            else:
                print("Quit Game")
                break



# 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 'q' 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")
        print("*************************************************************")
    elif score >4 or score<7:
        print("*************************************************************")
        print("You got",score,"out of 6.\n\nNice job! Your study payed off!\n")
        print("*************************************************************")


    end = False
    while end == False:


            response= input("Would you like to play again?\n"
                         "If yes enter 'yes or 'y' if not enter'n' or 'no' to quit:\n")
            if response.lower()=="no" or response.lower()=="n":
                print("You have quit the game. Thank you for playing",user_Name,":) ")
                keep_going = "end"
                break
            elif response.lower()=="yes" or response.lower()=="y":
                end = True
                print("Here is the Te Reo Maori Quiz again",user_Name,"\n")
                keep_going
            else:
                print("**Invalid input please enter 'y' if you want to play again\n or 'n' if you would like to quit game**\n")

1 个答案:

答案 0 :(得分:1)

一个问题是您正在检查错误函数的出口,答案被提供给int检查器,而strchecker仅给出错误。我已经移动了一些代码,并对其进行了一些简化-仍然可以对代码进行一些清理-不需要那么多的for循环。另外,问题/答案数组有点“笨拙”-在这里,我将它们全部放入一个二维数组(列表)中。

此外,我正在使用sys.exit()退出-而不是终止外部循环:

import sys

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


def checkans(question, low, high):
    valid = False
    while not valid:
        error = "Whoops! Please enter an integer between {} and {}\n".format(low, high)
        response = input("Please enter your answer or press 'q' to quit:\n")
        try:
            answer = int(response)

            if low <= answer <= high: 
                return answer
            else:
                print(error)
                print()

        except ValueError:
            if (response == 'q'):
                print("Quitting Game")
                sys.exit()
            else:
                print(error)

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


# GAME BEGINS HERE
def playGame():
    score = 0

    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 = checkname("Please enter your username:\n")
    print("Hi", user_Name,"Here is your first question:\n")

    # Get a question from the question list and print and loop one by one
    # LOOP THROUGH QUESTIONS
    for idx, question in enumerate(question_List):
        print(question[0])

        answer = checkans("Please enter in an answer or press 'q' to quit", 1,4)

        # Get answer and check if it is correct or incorrect by going to the list of correct answers 
        if answer == question[1]:
            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")
        print("*************************************************************")
    elif score >4 or score<7:
        print("*************************************************************")
        print("You got",score,"out of 6.\n\nNice job! Your study payed off!\n")
        print("*************************************************************")


    end = False
    while end == False:
        response = input("Would you like to play again?\nIf yes enter 'yes or 'y' if not enter'n' or 'no' to quit:\n")
        if response.lower() == "no" or response.lower() == "n":
            print("You have quit the game. Thank you for playing",user_Name,":) ")
            sys.exit()
        elif response.lower() == "yes" or response.lower() == "y":
            playGame()
        else:
            print("**Invalid input please enter 'y' if you want to play again\n or 'n' if you would like to quit game**\n")




playGame()