微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

Python 初学者:我想知道为什么我的附加列表中的最后一项没有打印出来

如何解决Python 初学者:我想知道为什么我的附加列表中的最后一项没有打印出来

我只是想知道是否有人能帮我弄清楚为什么我的附加列表中的最后一项没有打印出来。对不起,我只是一个初学者,我被卡住了,不知道该怎么做。如果有人可以提供帮助,将不胜感激。

#Round looping component version 3
#The Game Statistics and History was added to the code

这些是函数

import random

def check_how_many_questions():
    while True:
        #Ask the User how many questions they want to answer if they want to play the
        #option where they can choose how many questions they want
        #to answer of if they want to play the continuous question option
        response = input("How many questions do you want to answer: ")

        how_many_questions_error = "Please input either an integer that is more than 0 or <ENTER>."
#If ifinite mode is not chosen,check response is an integer more than 0
        if response != "":
            try:
                response = int(response)

#If response is too low,go back to the start of the loop and display an error message to help user
                if response < 1:
                    print(how_many_questions_error)
                    continue

            except ValueError:
                print(how_many_questions_error)
                continue

        return response

#Game Mode Input Checker Function
def game_mode_input_checker(question):
    while True:

        #Error message
        error_message = "Error please input an Integer between 1 for Addition questions"

        try:
            #It is a float input in the case the User inputs a valid input but just with a .0
            response = float(input(question))

            #If User's response is 1 return the response
            if response == 1:
                return response

            #If User's response is not 1,2,3,4 print the <ERROR> message
            else:
                print(error_message)
                print()
                continue

        #If the User inputs an invalid value,print the <ERROR> message
        except ValueError:
            print(error_message)
            print()

#Input Checker
def input_checker(question):
    while True:
        try:
            #It is a float input in the case the User inputs a valid input but just with a .0
            response = float(input(question))
            return response
        # Error message will be printed out to the User
        except ValueError:
            print("<ERROR> Please enter an Integer\n")
            continue

#Continue the game function
def continue_game(question):
    valid = False
    while not valid:
        response = input(question).lower()

        if response == "":
            return response

        elif response == "xxx":
            return response

        #If response is not "" or 'xxx' Error message will display to the User
        else:
            print()
            print("<Error> please enter either <Enter> or 'xxx'")
            print()

#Statement generator
#Decorates the statements in the Lucky Unicorn game
def statement_generator(statement,decoration):

    sides = decoration * 3

    statement = "{} {} {}".format(sides,statement,sides)
    top_bottom = decoration * len(statement)

    print(top_bottom)
    print(statement)
    print(top_bottom)

    return ""

这是主程序

#Main routine goes here

#Game Summary
game_summary = []

#Questions answered correct
questions_answered_correct = 0

#Questions answered incorrect
questions_answered_incorrect = 0

#Number of question answered
number_of_questions_answered = 0

game_loop = ""
while game_loop == "":

    user_choice_of_questions = check_how_many_questions()

    #Asks the User what game mode they want to play (1. Addition 2. Subtraction 3. Multiplication 4. Division)
    game_mode = game_mode_input_checker("What game mode do you want to play (1. Addition 2. Subtraction 3. Multiplication 4. Division)? ")

    while number_of_questions_answered < user_choice_of_questions:

        #If the User's Choice is <ENTER> the User has decided to answer the Continuous Question's option
        if user_choice_of_questions == "":
            heading = "Continuous Question: Question {}".format(number_of_questions_answered + 1)

        #If the User's Choice is not <ENTER> the User has decided to play the User Question number choose option
        else:
            heading = "Question {} of {}".format(number_of_questions_answered + 1,user_choice_of_questions)

        #Game Mode 1. Addition
        if game_mode == 1:

            #displays to the User the Question Number
            statement_generator(heading,"#")

            #Number 1 that is in the first position before
            #The addition is a random integer between 0 and 20
            number_1 = random.randint(0,20)
            #Number 2 that is in the first position before
            #The addition is a random integer between 0 and 20
            number_2 = random.randint(0,20)
            #The answer is equal to Number 1 + Number 2
            answer = number_1 + number_2

            #The question asked to the User
            #The Input Checker is a float input in the case the User input the correct answer but just with a .0
            response = input_checker("What is {} + {}?  ".format(number_1,number_2))

            #If the User's response is equal to the answer (Number 1 + Number 2) the User gets the question 'Correct'
            if response == answer:
                result = "Correct"
                question_outcome = "Question {} | Result: {} | Your Answer: {} | Correct Answer: {}".format(number_of_questions_answered + 1,result,response,answer)
                print()
                number_of_questions_answered += 1
                questions_answered_correct += 1


            #If the User's response is not equal to the answer (Number 1 + Number 2) the User gets the question 'Incorrect'
            else:
                result = "Incorrect"
                question_outcome = "Question {} | Result: {} | Your Answer: {} | Correct Answer: {}".format(number_of_questions_answered + 1,answer)
                print()
                number_of_questions_answered += 1
                questions_answered_incorrect += 1

                game_summary.append(question_outcome)


            print(question_outcome)

            #End game if the number of rounds has been played
            if number_of_questions_answered == user_choice_of_questions:
                number_of_questions_answered = 0
                break


            #If the number of rounds played is more than or equal to
            #One the User will be asked if they wish to continue the game or if they wish to
            #Quit the game they should input 'xxx'
            if number_of_questions_answered >= 1:
                print()
                game_loop = continue_game("Press <Enter> if you wish to continue the game,if you wish to quit type 'xxx': ")
                print()

            #If the User inputs 'xxx' when they are asked if they want to continue the game or not
            #The Game Loop will end
            #The number of questions the User answered will be reset to 0
            if game_loop == "xxx":
                number_of_questions_answered = 0
                break

            #Puts the question outcome in the game summary list
            game_summary.append(question_outcome)

这是不打印的附加列表

#The game summary is displayed to the User
#The game sumary includes the Question number and the question answered result
#The number of questions answered correct and incorrect is displayed to the User
#The percentage of how many questions the User has answered correct and how many questions they have answered incorrect
print()
print("*****Game History*****")
for game in game_summary:
    print(game)

print("Thank You for playing the Quiz Quest Game")

解决方法

在将最后一个结果附加到 game_summary 之前,结束条件中的中断会退出循环。删除它,你应该没问题。

if game_loop == "xxx":
    number_of_questions_answered = 0
    break <--- Here

编辑: 我刚刚看到您在多行上中断循环-> 在检查任何条件以中断循环之前附加结果-> 可能在您打印 question_outcome 之后

编辑2: 我刚刚注意到第二个 while 循环:在任何退出/中断检查之前保持中断并附加结果

,

好的,正如您已经告诉我的问题,最好的办法是移动最后几行中的 game_summary.append(question_outcome) 行。

所以一旦你得到答案就移动它。我这样做并得到了正确的结果 -

question_outcome = "Question {} | Result: {} | Your Answer: {} | Correct Answer: {}".format(number_of_questions_answered + 1,result,response,answer)
print()
game_summary.append(question_outcome)

更好的习惯是,使用新的 f-strings 而不是 .format(),因为它更具可读性 -

语法-

question_outcome = f"Question {number_of_questions_answered + 1} | Result: {result} | Your Answer: {response} | Correct Answer: {answer}"

您可以尝试将所有 .format 更改为 f-strings

,

您的代码非常冗长。如果您提供最少可重现的示例,您将更容易获得帮助。

关于你的问题。我会删除您代码的最后一行,而是在检查答案是对还是错后立即添加您附加到摘要列表的部分:

#Main routine goes here

#Game Summary
game_summary = []

#Questions answered correct
questions_answered_correct = 0

#Questions answered incorrect
questions_answered_incorrect = 0

#Number of question answered
number_of_questions_answered = 0

game_loop = ""
while game_loop == "":

    user_choice_of_questions = check_how_many_questions()

    #Asks the User what game mode they want to play (1. Addition 2. Subtraction 3. Multiplication 4. Division)
    game_mode = game_mode_input_checker("What game mode do you want to play (1. Addition 2. Subtraction 3. Multiplication 4. Division)? ")

    while number_of_questions_answered < user_choice_of_questions:

        #If the User's Choice is <ENTER> the User has decided to answer the Continuous Question's option
        if user_choice_of_questions == "":
            heading = "Continuous Question: Question {}".format(number_of_questions_answered + 1)

        #If the User's Choice is not <ENTER> the User has decided to play the User Question number choose option
        else:
            heading = "Question {} of {}".format(number_of_questions_answered + 1,user_choice_of_questions)

        #Game Mode 1. Addition
        if game_mode == 1:

            #Displays to the User the Question Number
            statement_generator(heading,"#")

            #Number 1 that is in the first position before
            #The addition is a random integer between 0 and 20
            number_1 = random.randint(0,20)
            #Number 2 that is in the first position before
            #The addition is a random integer between 0 and 20
            number_2 = random.randint(0,20)
            #The answer is equal to Number 1 + Number 2
            answer = number_1 + number_2

            #The question asked to the User
            #The Input Checker is a float input in the case the User input the correct answer but just with a .0
            response = input_checker("What is {} + {}?  ".format(number_1,number_2))

            #If the User's response is equal to the answer (Number 1 + Number 2) the User gets the question 'Correct'
            if response == answer:
                result = "Correct"
                question_outcome = "Question {} | Result: {} | Your Answer: {} | Correct Answer: {}".format(number_of_questions_answered + 1,answer)
                print()
                number_of_questions_answered += 1
                questions_answered_correct += 1


            #If the User's response is not equal to the answer (Number 1 + Number 2) the User gets the question 'Incorrect'
            else:
                result = "Incorrect"
                question_outcome = "Question {} | Result: {} | Your Answer: {} | Correct Answer: {}".format(number_of_questions_answered + 1,answer)
                print()
                number_of_questions_answered += 1
                questions_answered_incorrect += 1

            game_summary.append(question_outcome) # ADD HERE not within else 


            print(question_outcome)

            #End game if the number of rounds has been played
            if number_of_questions_answered == user_choice_of_questions:
                number_of_questions_answered = 0
                break


            #If the number of rounds played is more than or equal to
            #One the User will be asked if they wish to continue the game or if they wish to
            #Quit the game they should input 'xxx'
            if number_of_questions_answered >= 1:
                print()
                game_loop = continue_game("Press <Enter> if you wish to continue the game,if you wish to quit type 'xxx': ")
                print()

            #If the User inputs 'xxx' when they are asked if they want to continue the game or not
            #The Game Loop will end
            #The number of questions the User answered will be reset to 0
            if game_loop == "xxx":
                number_of_questions_answered = 0
                break

            #Puts the question outcome in the game summary list
            # game_summary.append(question_outcome) # REMOVE!


版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。