Python Forum
Matching variable to a list index - Printable Version

+- Python Forum (https://python-forum.io)
+-- Forum: Python Coding (https://python-forum.io/forum-7.html)
+--- Forum: General Coding Help (https://python-forum.io/forum-8.html)
+--- Thread: Matching variable to a list index (/thread-31206.html)

Pages: 1 2


RE: Matching variable to a list index - Gilush - Nov-28-2020

(Nov-28-2020, 11:31 AM)perfringo Wrote: Can you describe in spoken language what you are trying to do/achieve?

Without a plan it’s hard to judge have you reached your destination or not (or are you moving in right direction with optimal path).

Imagine this scenrario:

you are a tech professor and you want to build a test for your students.
using this program you can write your question, choose how many answers it will have, type your own answers and then you choose the right answer. the right answer is stored in a list (or dict) that will be printed out at the end along with the question number, question, answer, score.

on later stages i'll add the student's side with username/password db, saving/exporting options, GUI with tkinter and more.


Thanks again for your time.


RE: Matching variable to a list index - Gilush - Nov-28-2020

Ok so now i've managed to match using dictionaries but for some reason the dict_right_answers stops updating after the 2nd answer and keeps overwriting the last key.

Thoughts?

def get_questions():

    question = input("Enter your question: ")
    score = int(input("Enter question score: "))

    try:
        if sum(scores + [score]) > 100:
            print("Score average is higher than 100")
            if len(question_list) > 0:
                question_list.pop()
            if len(scores) > 0:
                scores.pop()

    except ValueError:
        print("Use Numbers.")
        get_questions()

    question_list.append(question)
    scores.append(score)


def get_answers():

    global answer
    choice_number = 0

    try:
        choice_number = int(input("Number of answers (Max 4)? "))

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

    try:
        if choice_number > 4 or choice_number < 2:
            print("Please select [2-4]")
            choice_number = 0
            question_answers_global.clear()
            question_answers.clear()
            get_answers()

    except choice_number < 2 or choice_number > 4:
        print("Error")

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

        dict_answers[ans + 1] = answer
        answer_number.append(ans + 1)
        question_answers.append(answer)

    answers.clear()
    answers.append(choice_number)

    print(f"Number of answers: {answers[0]}")
    print(dict_answers)
    question_answers.clear()


def get_right_answer():

    right_answer = 0

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

    except ValueError:
        print("Value Error!")

    print(f"Your choice: {right_answer}")
    if 0 < right_answer <= answers[0]:
        sure = input("Are you sure? [Y/n]: ")
        if sure.lower() == "y":
            dict_right_answers[right_answer] = dict_answers[right_answer]
            print(f"Questions: {question_list}")
            print(f"Right Answers: {dict_right_answers}")
            print(f"Total Score: {sum(scores)}")

        elif sure.lower() == "n":
            get_right_answer()

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

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


def main():

    get_questions()
    get_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":
        dict_answers.clear()
        answers.clear()
        main()
        finish()

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

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


if __name__ == "__main__":
    global answer

    scores = []
    question_list = []
    question_list_global_dict = {}
    question_answers = []
    question_answers_global = []
    answers = []
    answer_number = []
    right_answer_number = []
    dict_answers = {}
    dict_right_answers = {}

    main()
    finish()
[Image: missing.png]


RE: Matching variable to a list index - bowlofred - Nov-28-2020

Did you actually understand the issues I raised as 1, 2, and 3?


RE: Matching variable to a list index - Gilush - Nov-28-2020

(Nov-28-2020, 06:46 PM)bowlofred Wrote: Did you actually understand the issues I raised as 1, 2, and 3?

Yes I have and that's why I got it to work but I guess i'm half right here or i'm missing something very obvious.


RE: Matching variable to a list index - AntonioBro - Nov-29-2020

Selenium is a portable framework for testing web applications. Selenium provides a playback tool for authoring functional tests without the need to learn a test scripting language (Selenium IDE)

Selenium Training in Chennai


RE: Matching variable to a list index - Larz60+ - Nov-29-2020

use the tutorial here for selenium.
It's short, and to the point.
web scraping part 1
web scraping part 2


RE: Matching variable to a list index - Gilush - Nov-29-2020

(Nov-29-2020, 04:10 PM)Larz60+ Wrote: use the tutorial here for selenium.
It's short, and to the point.
web scraping part 1
web scraping part 2

how is that related to anything I wrote or the code?
this is no way related to web.


RE: Matching variable to a list index - Larz60+ - Nov-30-2020

It doesn't. Sorry about that. I read the post directly above mine, which mentioned selenium, and that threw me off on a tangent.