Python Forum
How to print multiple elements from multiple lists in a FOR loop?
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
How to print multiple elements from multiple lists in a FOR loop?
#1
Question 
This is a followup post to my previous one - I decided to re-write the code and now i'm facing a new problem.

I'm working on a dynamic, text based (for now), Test Maker program. The user gets to write the question, choose how many answers it will have, what score to give for it, choose the right question and print the results in a table form.

How can I write a for loop (not one-liner) so I can go through the lists and print the output according to the table?

here's the function where I want to place it:

def finish(lines):

    print("\n\n==============RESULTS==============\n")
    print("#\t Question\t Answer\t\t Score")
    number_of_questions = len(question_list)
here's the result table template I wish to present:

[Image: DlKT8vw.png]

and here's the full code:

def get_questions():

    question = input("Enter your question: ")
    if len(question) == 0:
        print("No Empty Questions.")
        get_questions()

    question_list.append(question)

    return question


def get_question_score(s):

    try:
        s = int(input("Enter Question Score: "))

    except ValueError:
        print("[!]Error! Use numbers.")

    return s


def check_sum(s):

    scores_local.clear()

    try:
        if sum(scores) + s > 100:
            print("Score sum is higher than 100")
            print(f"{100 - sum(scores)} left")
            score = 0
            if len(scores) != 0:
                scores.pop()
            return False

    except ValueError:
        print("Use Numbers.")

    scores_local.append(s)
    scores.append(s)

    return True


def get_number_of_answers(n_o_a):

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

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

    number_of_answers.append(n_o_a)

    return n_o_a


def big_small(n_o_a):

    if n_o_a < 2 or n_o_a > 4:
        print("Please select [2-4]")
        answers.clear()
        answers_local.clear()
        big_small(n_o_a=number_of_answers[0])
        return False

    elif 2 > number_of_answers[0] > 4:
        print("Error! Please select [2-4]")
        return False

    else:
        print(n_o_a)
        return True


def get_answers(num_of_answers, answer):

    answers_local.clear()
    answers.clear()

    for ans in range(number_of_answers[0]):
        answer = input(f"Answer #{ans+1}: ")    # Start indexing from 1
        if len(answer) == 0:
            print("Error! No Input!")
            get_answers(num_of_answers, answer="")

        answers_local.append(answer)
        answers.append(answer)

    return answer


def get_right_answer_number(num):

    try:
        num = int(input("Type the Right Answer Number: "))

    except ValueError:
        print("Error! Please type numbers only.")

    return num


def check_right_answer_number(num):

    if 0 < num <= number_of_answers[0]:
        return True

    else:
        return False


def ask_if_sure(ans):   # Returns True

    ask_sure = input("Are you sure? [Y/n]: ")
    if ask_sure.lower() == "n":
        number_of_answers.clear()
        check_right_answer_number(num=number_of_answers[0])

    elif ask_sure.lower() == "y":
        return True

    else:
        print("Error! Please type [Y/n]")
        ask_if_sure(ans="")


def ask_continue():

    a_continue = input("Do you wish to add more questions? [Y/n]: ")
    if a_continue.lower() == "y":
        return True

    elif a_continue.lower() == "n":
        return False

    else:
        print("Error! Please choose [Y/N]")
        ask_continue()


def restart():

    answers_local.clear()
    number_of_answers.clear()
    right_answer_number.clear()
    main()


def finish(lines):

    print("\n\n==============RESULTS==============\n")
    print("#\t Question\t Answer\t\t Score")
    number_of_questions = len(question_list)


def main():

    questions = get_questions()
    question_score = get_question_score(s=0)
    score_sum = check_sum(question_score)   # Returns True/False
    num_of_answers = get_number_of_answers(n_o_a=0)
    is_bigger = big_small(num_of_answers)   # Returns True/False

    if not is_bigger:
        print(f"Question score: {question_score}, Valid: {score_sum}")
        if sum(scores) != 0:
            scores.pop()
            question_score = get_question_score(s=0)

    while is_bigger:

        question = questions[-1]
        multiple_answers = get_answers(num_of_answers, answer="")
        print(answers_local)
        right_answer_number_var = get_right_answer_number(num=0)
        print(right_answer_number_var)
        is_right = check_right_answer_number(right_answer_number_var)

        if not is_right:
            print(f"Error! Please select [1={number_of_answers[0]}")
            right_answer_number_var = get_right_answer_number(num=0)

        while is_right:
            sure = ask_if_sure(ans="")

            while sure:
                if number_of_answers[0] < len(questions):
                    lines = len(questions)
                else:
                    lines = number_of_answers[0]

                answers.append(right_answer_number_var)
                ask_if_continue = ask_continue()
                if not ask_if_continue:
                    finish(lines)

                while ask_if_continue:
                    restart()
                    break
                break
            break
        break


if __name__ == "__main__":

    question_list = []
    scores = []
    scores_local = []
    number_of_answers = []
    answers = []
    answers_local = []
    right_answer_number = []

    main()
Thanks for your time!
Reply


Messages In This Thread
How to print multiple elements from multiple lists in a FOR loop? - by Gilush - Dec-01-2020, 09:48 PM

Possibly Related Threads…
Thread Author Replies Views Last Post
  __init__() got multiple values for argument 'schema' dawid294 4 1,887 Jan-03-2024, 09:42 AM
Last Post: buran
  problem with print lists MarekGwozdz 4 612 Dec-15-2023, 09:13 AM
Last Post: Pedroski55
  python convert multiple files to multiple lists MCL169 6 1,436 Nov-25-2023, 05:31 AM
Last Post: Iqratech
  Multiple variable inputs when only one is called for ChrisDall 2 449 Oct-20-2023, 07:43 PM
Last Post: deanhystad
  Can I use logging in a class (without multiple messages) mevan 2 535 Oct-16-2023, 11:08 PM
Last Post: mevan
  Search for multiple unknown 3 (2) Byte combinations in a file. lastyle 7 1,257 Aug-14-2023, 02:28 AM
Last Post: deanhystad
Question Using SQLAlchemy, prevent SQLite3 table update by multiple program instances Calab 3 703 Aug-09-2023, 05:51 PM
Last Post: Calab
  What's the best way for multiple modules to handle database activity? SuchUmami 3 611 Jul-08-2023, 05:52 PM
Last Post: deanhystad
  splitting file into multiple files by searching for string AlphaInc 2 815 Jul-01-2023, 10:35 PM
Last Post: Pedroski55
  Plot multiple 2D Curves in 3D stc 5 893 Jun-11-2023, 05:48 PM
Last Post: snippsat

Forum Jump:

User Panel Messages

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