pythonturtle-graphicspython-turtle

not getting the final score... python turtle coding


When I try run this code, everything seems to work but when getting the final score, I don't get a score as it only appears blank.

#!/usr/bin/env python3
import json #this is to load the files of from the target folder
import time
import turtle

TOPICS_LIST = ['General_Questions', 'Disciplines', 'Studies', 'Psychology', 'Theories'] 
# this list has to in sync with the JSON filename and the Menu prompt inside test() method

def quiz_main():
    turtle.clear()
    turtle.hideturtle()
    turtle.penup()
    screen = turtle.Screen()
    screen.setup(1000,600)
    screen.colormode(255)
    screen.bgcolor(255, 255, 255)

def ask_one_question(question):
    quiz_main()
    turtle.goto(-400, 50)
    turtle.write("\n" + question, font=('Courier', 20, 'bold'))
    time.sleep(1)
    choice = str(turtle.textinput("Enter Your Choice [a/b/c/d]: ", "Answer"))
    while(True):
        if choice.lower() in ['a', 'b', 'c', 'd']:
            return choice
        else:
            turtle.write("Incorrect choice. Please try again", font=('Courier', 20, 'italic'))
            choice = str(turtle.textinput("Enter Your Choice [a/b/c/d]: ", "Answer"))

def score_one_result(key, meta):
    quiz_main()
    turtle.goto(-300, 100)
    actual = meta["answer"]
    if meta["user_response"].lower() == actual.lower():
        return 2
    else:
        return -1

def test(questions):
    quiz_main()
    turtle.goto(-450, -100)
    score = 0
    turtle.write("Please read the instructions:\n1. Please enter only your choice letter corresponding to your answer.\n2. Each question has 2 points\n3. A wrong answer will give -1 \n The Quiz will start shortly.\n Good Luck!\n", font=('Courier', 15, 'bold'))
    time.sleep(10)
    quiz_main()
    turtle.goto(-400, -100)
    for key, meta in questions.items():
        questions[key]["user_response"] = ask_one_question(meta["question"])
    quiz_main()
    turtle.goto(-200, 0)
    for key, meta in questions.items():
        score += score_one_result(key, meta)
    turtle.write("Your Final Score:", score, font=('Courier', 30, 'bold'))
    turtle.exitonclick()

def load_question(filename):
    """
    loads the questions from the JSON file into a Python dictionary and returns it
    """
    questions = None
    with open(filename, "r") as read_file:
        questions = json.load(read_file)
    return (questions)

def play_quiz():
    quiz_main()
    turtle.goto(-300, -50)
    flag = False
    try:
        turtle.write("Thank you for taking Our Simple Quiz!\nChoose which one is your interest:\n(1). General Questions\n(2). Disciplines\n(3). Studies\n(4). Psychology\n(5). Theories\nEnter Your Choice [1/2/3/4/5]: ", font=('Courier', 20, 'bold'))
        time.sleep(5)
        choice = int(turtle.textinput("Topic", "Topic"))
        if choice > len(TOPICS_LIST) or choice < 1:
            print("Invalid Choice. Try Again")
            flag = True # raising flag
    except ValueError as e:
        print("Invalid Choice. Try Again")
        flag = True # raising a flag
    if not flag:
        questions = load_question('c:/Quiz Game/topics/'+TOPICS_LIST[choice-1]+'.json')
        test(questions)
    else:
        play_quiz() # replay if flag was raised

def user_begin_prompt():
    quiz_main()
    turtle.goto(-200, 0)
    turtle.write("Simple Quiz Game", font=('Courier', 30, 'bold'))
    time.sleep(5)
    play = turtle.textinput("Shall we try?\nA. Yes\nB. No", "A or B")
    if play.lower() == 'a' or play.lower() ==  'y':
        play_quiz()
    elif play.lower() == 'b':
        print("Hope you come back soon!")
    else:
        print("Hmm. I didn't quite understand that.\nPress A to play, or B to quit.")
        user_begin_prompt()
        
def execute():
    user_begin_prompt()

if __name__ == '__main__':
    execute()

I already tried changing variable to str or int but same issue. there is JSON file which has the questions is properly formatted and works well with no issues. the code works 90% but only the final score is not showing.

I'm hoping to show the final score of the user doing this test.

here is the json code for the topics:

{
    "1": {
    "question": "What is the main focus of the HUMSS strand?\n(a) Computer Science\n(b) Humanities and Social Sciences\n(c) Information Technology\n(d) Coding\n",
    "answer": "b",
    "more_info": "None",
    "user_response": "None"
    },
}

this is a school project that me and my classmates are working so we can pass our computer class. Thank you for your assistance. I am just new to python.


Solution

  • This line of code is the problem:

    turtle.write("Your Final Score:", score, font=('Courier', 30, 'bold'))
    

    Specifically, the issue is that you're passing two separate arguments to be written. That works with other functions such as print(), but it does not work here.

    turtle.write() accepts only one argument for text to be written.