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
#8
I see, you have at least two problems with the shown code.

1) your exception check in get_right_answer() should only be a couple lines long, but the rest of the function (lines 63-80) is in that block, so it can't get executed if the answer is input properly.
2) Once you fix that, line 69 is question_right_answer[right_answer] = answer. You're assigning "answer", not "right_answer". That seems wrong to me.
3) your printout from line 103 is print(f"{i+1}\t {question_list[i]}\t\t {answer}\t\t {scores[i]}"). Note all the columns depend on i except for the third. That one is just the bare answer, so it will be the same for every line.


An example of looping until you get a valid response:

valid = "no"
while not valid.startswith(("y", "Y")):
    right_answer = int(input("Right Answer: "))
    if 0 < right_answer <= answers[0]:
        valid = input("Are you sure? [Y/n]")
        valid = valid or "Y"
    else:
        print(f"Please select 1-{answers}")

# We don't pass this point until user affirms answer is okay.  This
# separates the input code from the other tasks.
#  print the right answer to the screen for the user to see
#  store the right answer for later use..
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 bowlofred - Nov-28-2020, 04:00 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 785 Jan-20-2024, 09:20 PM
Last Post: Learner1
Thumbs Down I hate "List index out of range" Melen 20 3,627 May-14-2023, 06:43 AM
Last Post: deanhystad
  IndexError: list index out of range dolac 4 2,028 Jul-25-2022, 03:42 PM
Last Post: deanhystad
  Split string using variable found in a list japo85 2 1,388 Jul-11-2022, 08:52 AM
Last Post: japo85
  IndexError: list index out of range Anldra12 2 1,514 May-03-2022, 01:39 PM
Last Post: Anldra12
  IndexError: list index out of range rf_kartal 6 2,977 Sep-07-2021, 02:36 PM
Last Post: Larz60+
  Python Error List Index Out of Range abhi1vaishnav 3 2,451 Sep-03-2021, 08:40 PM
Last Post: abhi1vaishnav
  An IF statement with a List variable dedesssse 3 8,626 Jul-08-2021, 05:58 PM
Last Post: perfringo
  IndexError: list index out of range Laplace12 1 2,281 Jun-22-2021, 10:47 AM
Last Post: Yoriz
  IndexError: list index out of range brunolelli 11 6,800 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