IndexError:使用tkinter和过程列出索引超出范围

时间:2016-01-19 04:07:41

标签: python python-2.7 tkinter

我最近正在调整tkinterPython一起使用,作为作业的一部分,我被要求制作基于tkinter的程序。
我决定尝试一个有三个困难的测验,并且可以从可用于该难度的列表中随机选择一个问题。
当我运行程序并选择难度(简单就是我到目前为止所做的一切)它继续运行但我在IDE中出错:

Exception in Tkinter callback
Traceback (most recent call last):
  File "C:\Python27\lib\lib-tk\Tkinter.py", line 1536, in __call__
    return self.func(*args)
  File "E:\Unit 6 - Software Development\randomtkintergame.py", line 98, in easy_mode
question, answer, wrong_1, wrong_2, wrong_3 = easy[question_choice]
IndexError: list index out of range

这是我的代码:

import Tkinter as tk
from tkSimpleDialog import *
import math
import string
import time
from random import randint

global question_title, button_1, button_2, button_3, button_4, question_number, score_count, easy, question_choice, question, answer, wrong_1, wrong_2, wrong_3
question_number = 1
score_count = 0
question_choice = 0
question = 0
answer = 0
wrong_1 = 0
wrong_2 = 0
wrong_3 = 0

easy = [("What is the longest river in the world?", "The River Nile", "The Amazon River", "The River Humber", "The River Trent"),
    ("What is Doctor Who`s time box called?", "TARDIS", "TIMEY-WIMEY", "TASDIR", "WIMEY-TIMEY"),
    ("How many faces are on a die?", "6", "5", "7", "4"),
    ("How many wives did Henry VIII have?", "6", "8", "4", "9"),
    ("What is the square root of 169?", "13", "11", "17", "19"),
    ("In a game of chess, what is the only piece able to jump over other pieces?", "Knight", "Pawn", "Bishop", "All"),
    ("Who is the author of the `Harry Potter` books?", "J.K. Rowling", "J.R.R. Tolkien", "George R.R. Martin", "Julius Caesar"),
    ("What is the name of the clockwork device used by musicians to measure time?", "Metronome", "Tuner", "Amplifier", "Time Measurer"),
    ("Which two colours are Dennis the Menace`s jumper?", "Red and black", "Blue and black", "White and gold", "Red and blue"),
    ("An octagon has how many sides?", "8", "6", "10", "7"),
    ("Which sign of the zodiac is represented by the Ram?", "Aries", "Scorpio", "Ophiuchus", "Aquarius"),
    ("Which animal is associated with the beginning of an MGM film?", "A lion", "An alpaca", "A very small albatross", "A tiger"),
    ("What was the hunchback of Notre Dame`s name?", "Quasimodo", "Esmerelda", "Frollo", "Not Re Dame"),
    ("Who is the animated star of the computer game Tomb Raider?", "Lara Croft", "Sara Craft", "Tom Cruise", "Bill Gates"),
    ("What is the name of the city in which The Simpsons live?", "Springfield", "Quahog", "South Park", "Boston"),
    ("In which film would you first have come across the character of Marty McFly?", "Back to the Future", "Lord of the Rings", "The IT Crowd", "Harry Potter"),
    ("How many years are there in a millennium?", "1,000", "10,000", "100", "1,000,000"),
    ("In Greek mythology, what was Medusa`s hair made of?", "Snakes", "Threads of silk", "Stone", "Leeches"),
    ("What is the first letter of the Greek alphabet?", "A - Alpha", "B - Beta", "G - Gamma", "E - Epsilon"),
    ("What type of animal was Stuart, in the 1999 film `Stuart Little`?", "Mouse", "Frog", "Guinea pig", "Porcupine"),
    ("What creature appears on the flag of Wales?", "Dragon", "Alligator", "Crocodile", "Lizard"),
    ("On what part of the body would you wear a `sombrero`?", "Head", "Feet", "Hands", "Chest"),
    ("How many wheels are on a tricycle?", "3", "2", "6", "8"),
    ("Oxygen and which other element makes up water?", "Hydrogen", "Helium", "Ytterbium", "Einsteinium"),
    ("How many inches are in a yard?", "36", "12", "8", "24"),
    ("What colour is an emerald?", "Green", "Black", "Orange", "White")]


def easy_mode():
    global button_1, button_2, button_3, button_4, question_number, question_title, score_count, easy, question_choice, question, answer, wrong_1, wrong_2, wrong_3
    repeat = True
    while repeat == True:
      for i in range (0, len(easy)): #the code works through all the questions
        question_choice = randint(0, 25)#generate random int
        question, answer, wrong_1, wrong_2, wrong_3 = easy[question_choice]
        easy.pop(question_choice)

root = tk.Tk()

root.geometry("700x500")
root.title("Educational Quiz")
root.configure(background="#f2e5ff")


question_title = tk.Label(root, text="Please select a difficulty.", relief=GROOVE, bd=5, bg="#66b2ff", fg="black", font=("Calibri Light", 20))
question_title.place(x = 50, y = 25, width=625, height=80)




button_1 = tk.Button(root, text = "EASY", relief=GROOVE, bd=5, command = easy_mode, bg="#0055ff", fg="black", font=("Calibri Light", 20))
button_1.place (x = 50, y = 180 , width=300, height=80)

当然,它并不是整个代码,因为我并不想粘贴与问题实际无关的内容,但如果需要,我可以提供其余内容。
(另外,我可能不需要设置所有全局变量,但我会在需要时处理它。)

1 个答案:

答案 0 :(得分:2)

问题是你在缩小列表的同时不断选择原始范围,直到最终得到的索引太大。您可以用

替换while循环
while easy:
    question_choice = randint(0, len(easy)-1)  #generate random int
    question, answer, wrong_1, wrong_2, wrong_3 = easy[question_choice]
    easy.pop(question_choice)

但我认为,更好的是以随机顺序呈现所有问题,

random.shuffle(easy)
for q_and_a in easy:
    question, answer, wrong_1, wrong_2, wrong_3 = q_and_a

两种替换品都未经过测试,因此可能存在拼写错误。