简体   繁体   中英

Need help in identifying the mistake in my code. Tkinter messagebox not displaying desired output

def play():
    wind2 = tk.Toplevel()
    v = tk.IntVar()
    ques = ["Identify the least stable ion amongst the following.",
            "The set representing the correct order of first ionisation potential is",
            "The correct order of radii is"]
    o1 = ["", "Li⁺", "Be⁻", "B⁻", "C⁻"]
    o2 = ["", "K > Na > Li", "Be > Mg >Ca", "B >C > N", "Ge > Si >C"]
    o3 = ["", "N < Be < B", "F⁻ < O²⁻ < N³⁻", "Na < Li < K", "Fe³⁺ < Fe⁴⁺ < Fe²⁺"]
    choice = [o1, o2, o3]
    ans = [2, 2, 2]
    user_ans = []
    
    def selection():
        selected = v.get()
        for i in range(len(ques)):  
            if qsn["text"] == ques[i]:  
                break    
        if ans[i] == selected:
            print("Correct Answer")
            user_ans.append((i, selected))

    global score
    score = len(user_ans)*10
    def nxt():
        nxt.count += 1
        name.destroy()
        nmbox.destroy()
        nmbut.destroy()
        n = random.randint(0,2)
        qsn['text'] = ques[n]
        qsn.pack()
        r1['text'] = choice[n][1]
        r2['text'] = choice[n][2]
        r3['text'] = choice[n][3]
        r4['text'] = choice[n][4]
        r1.pack()
        r2.pack()
        r3.pack()
        r4.pack()
        nbut.pack()
        if nxt.count > 3:
            messagebox.showinfo("score", str(score))
    nxt.count = 0

    name = tk.Label(wind2, text = "Enter your name below:")
    nmbox = tk.Entry(wind2, bd = 4)
    nmbut = tk.Button(wind2, text = "Go", command = nxt)
    name.pack()
    nmbox.pack()
    nmbut.pack()
    
    qsn = tk.Label(wind2)
    r1 = tk.Radiobutton(wind2, variable = v, value = 1, command=selection)
    r2 = tk.Radiobutton(wind2, variable = v, value = 2, command=selection)
    r3 = tk.Radiobutton(wind2, variable = v, value = 3, command=selection)
    r4 = tk.Radiobutton(wind2, variable = v, value = 4, command=selection)
    nbut = tk.Button(wind2, text = "next", command = nxt)

After selecting the correct option, (ie when ans[i]=selected) the length of user_ans is of course not 0. Then why does the messagebox return score as 0 everytime? I am unable to figure out if I have made any mistake. Basically this is a quiz application. Score of the user is 10 times the number of correct answers.

First, please provide a reproducible code sample next time. There are a lot of logic errors in your script. First you calculate your score in the beginning when you run the play function. At that point your list user_ans is still empty. If you move your score calculation into the nxt function, you end up with different issues, for example your score can go into infinity when someone keeps clicking the right answer over and over. So you should evaluate the user selection only once the user clicks next .

Here is some rearrangement. Still this is not ideal, but you can work from there.

import tkinter as tk
import random
from tkinter import messagebox


def play():
    v = tk.IntVar()
    ques = ["Identify the least stable ion amongst the following.",
            "The set representing the correct order of first ionisation potential is",
            "The correct order of radii is"]
    o1 = ["", "Li⁺", "Be⁻", "B⁻", "C⁻"]
    o2 = ["", "K > Na > Li", "Be > Mg >Ca", "B >C > N", "Ge > Si >C"]
    o3 = ["", "N < Be < B", "F⁻ < O²⁻ < N³⁻", "Na < Li < K", "Fe³⁺ < Fe⁴⁺ < Fe²⁺"]
    choice = [o1, o2, o3]
    ans = [2, 2, 2]
    user_ans = []


    def selection():
        global selected
        selected = v.get()


    def nxt():
        nxt.count += 1
        name.destroy()
        nmbox.destroy()
        nmbut.destroy()
        n = random.randint(0,2)
        qsn['text'] = ques[n]
        qsn.pack()
        r1['text'] = choice[n][1]
        r2['text'] = choice[n][2]
        r3['text'] = choice[n][3]
        r4['text'] = choice[n][4]
        r1.pack()
        r2.pack()
        r3.pack()
        r4.pack()
        nbut.pack()

        for i in range(len(ques)):  
            if qsn["text"] == ques[i]:  
                break
        if ans[i] == selected:
            print("Correct Answer")
            user_ans.append(i)

        if nxt.count > 3:
            score = len(user_ans)*10
            messagebox.showinfo("score", str(score))

    nxt.count = 0

    name = tk.Label(root, text = "Enter your name below:")
    nmbox = tk.Entry(root, bd = 4)
    nmbut = tk.Button(root, text = "Go", command = nxt)
    name.pack()
    nmbox.pack()
    nmbut.pack()
    
    qsn = tk.Label(root)
    r1 = tk.Radiobutton(root, variable = v, value = 1, command=selection)
    r2 = tk.Radiobutton(root, variable = v, value = 2, command=selection)
    r3 = tk.Radiobutton(root, variable = v, value = 3, command=selection)
    r4 = tk.Radiobutton(root, variable = v, value = 4, command=selection)
    nbut = tk.Button(root, text = "next", command = nxt)

root = tk.Tk()
play()
root.mainloop()

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM