Python Forum
Matching variable to a list index
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Matching variable to a list index
#1
Question 
Hi! :)

I'm exercising with lists and dictionary by creating a test maker.
The point of the program is to be as dynamic as possible, meaning, the user can choose how many answers each question will have.
When I print the results I get the wrong answers near the question numbers.

What am I missing with the list/dictionary indexing? Is there more efficient way to get the right results?

code snippet:

def get_answers():

    global answer
    choice_number = 0

    try:
        choice_number = int(input("Number of answers? "))
        if choice_number > 4 or choice_number < 2:
            print("Please select [2-4]")
            choice_number = 0
            get_answers()

    except ValueError:
        print("Please select [2-4]")
        get_answers()

    for ans in range(choice_number):
        answer = input(f"Answer {ans + 1}: ")
        if len(answer) == 0:
            print("No empty answers.")
            get_answers()

        question_answers.append(answer)

    answers.append(choice_number)
    print(f"Number of answers: {answers}")
    print(question_answers)
    q_answers.append(answer)
    question_answers.clear()


def get_right_answer():

    right_answer = 0

    try:
        right_answer = int(input("Right Answer: "))

    except ValueError:
        print("Value Error!")
        get_right_answer()

        if 0 < right_answer <= answers[0]:
            sure = input("Are you sure? [Y/n]: ")
            if sure.lower() == "n":
                get_right_answer()

            elif sure.lower() == "y":
                question_right_answer[right_answer] = answer
                print(f"Questions: {question_list}")
                print(f"Right Answers: {question_right_answer}")
                print(f"Total Score: {sum(scores)}")

            else:
                print("Please Select [Y/n]")
                get_right_answer()

        else:
            print(f"Please select 1-{answers}")
            get_right_answer()
def finish():

    question_num = len(question_list)
    ask_continue = input("Do you wish to continue? [Y/n]: ")
    if ask_continue.lower() == "y":
        answers.pop()
        main()
        finish()

    elif ask_continue.lower() == "n":
        print("\n\n==============RESULTS==============\n")
        print("#\t Question\t Answer\t Score")
        for i in range(question_num):
            print(f"{i+1}\t {question_list[i]}\t\t {answer}\t\t {scores[i]}")
            i += 1

    else:
        print("Please select [Y/n]")
        finish()


if __name__ == "__main__":

    global answer

    scores = []
    question_list = []
    q_answers = []
    answers = []
    question_answers = []
    question_right_answer = {}
    question = input("Enter your question: ")
    score = int(input("Enter question score: "))

    main()
    finish()
here's a screenshot of the entire output:
[Image: 5VCTs.png]

Thanks for your time.
again, this is not homework or any must assignment, this is just for me to stay in shape.
Reply


Messages In This Thread
Matching variable to a list index - by Gilush - Nov-28-2020, 01:28 AM
RE: Matching variable to a list index - by Gilush - Nov-28-2020, 01:51 AM
RE: Matching variable to a list index - by Larz60+ - Nov-28-2020, 02:04 AM
RE: Matching variable to a list index - by Gilush - Nov-28-2020, 02:10 AM
RE: Matching variable to a list index - by Gilush - Nov-28-2020, 02:29 AM
RE: Matching variable to a list index - by Gilush - Nov-28-2020, 11:05 AM
RE: Matching variable to a list index - by Gilush - Nov-28-2020, 11:42 AM
RE: Matching variable to a list index - by Gilush - Nov-28-2020, 05:58 PM
RE: Matching variable to a list index - by Gilush - Nov-28-2020, 07:18 PM
RE: Matching variable to a list index - by Larz60+ - Nov-29-2020, 04:10 PM
RE: Matching variable to a list index - by Gilush - Nov-29-2020, 09:44 PM
RE: Matching variable to a list index - by Larz60+ - Nov-30-2020, 01:06 AM

Possibly Related Threads…
Thread Author Replies Views Last Post
  Variable for the value element in the index function?? Learner1 8 633 Jan-20-2024, 09:20 PM
Last Post: Learner1
Thumbs Down I hate "List index out of range" Melen 20 3,307 May-14-2023, 06:43 AM
Last Post: deanhystad
  IndexError: list index out of range dolac 4 1,900 Jul-25-2022, 03:42 PM
Last Post: deanhystad
  Split string using variable found in a list japo85 2 1,295 Jul-11-2022, 08:52 AM
Last Post: japo85
  IndexError: list index out of range Anldra12 2 1,437 May-03-2022, 01:39 PM
Last Post: Anldra12
  IndexError: list index out of range rf_kartal 6 2,836 Sep-07-2021, 02:36 PM
Last Post: Larz60+
  Python Error List Index Out of Range abhi1vaishnav 3 2,302 Sep-03-2021, 08:40 PM
Last Post: abhi1vaishnav
  An IF statement with a List variable dedesssse 3 8,210 Jul-08-2021, 05:58 PM
Last Post: perfringo
  IndexError: list index out of range Laplace12 1 2,219 Jun-22-2021, 10:47 AM
Last Post: Yoriz
  IndexError: list index out of range brunolelli 11 6,496 Mar-25-2021, 11:36 PM
Last Post: brunolelli

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020