ValueError:list.remove(x):x不在列表中错误如何修复?

问题描述 投票:0回答:1

我有一些代码,它通过一个for循环,应该给我和tkinter窗口中的9行和9列按钮输出。这些按钮应该有一个随机数,范围从1到9.但我不希望同一列和行中的相同数字相同。

为了实现这一点,我尝试了.pop []和.remove()以及del但没有一个正常工作。我得到错误row1.remove(“4”)ValueError:list.remove(x):x不在列表中。当我尝试删除该数字时,同一行中有2个相同的数字。有人可以帮忙吗?

import tkinter as tk
import random
row1 = ["1","2","3","4","5","6","7","8","9"]
col1 = ["1","2","3","4","5","6","7","8","9"]
button = ["1","2","3","4","5","6","7","8","9"]
random.shuffle(button)
root = tk.Tk()
i = 0
for x in range(9):
    for y in range(9):
        number = random.choice(button)
        btn = tk.Button(text=number, bg="white", activebackground="black", 
width=2)
        btn.grid(row=y, column=x)
        i += 1
        print(number)
        if number == "1":
            row1.remove("1")
            col1.remove("1")
        elif number == "2":
            row1.remove("2")
            col1.remove("2")

顺便说一下,elif一直到第9位。我只是不想在这里全部。

我希望输出为9 x 9网格,所有网格都包含1到9之间的随机数,并且行和列中的数字都不相同。

python random tkinter
1个回答
1
投票

下面带有内联代码的脚本解释了很多。由于您的矩阵是9x9我使用了行* columns = 81,因为您可以将它们全部ID。如果只有9个值,你仍会得到一些双打。通过调整unique_value轻松调整。享受简约; P

import tkinter as tk
import random

rows       = 9
columns    = 9

unique_IDs = 9   # unique IDs per row. If value below e.g. rows 
                 # then backup button list is created to prevent
                 # an empty list below. Value larger than zero.

root = tk.Tk()
i = 0

# if matrix == unique IDs the amount (rows * columns)IDs is created.
if rows * columns == unique_IDs:

    # if unique IDs are smaller than row values below code 
    # compensates for it by taking total positions in rows as base.                          
    if unique_IDs < rows:
        button = [*range(1, ((rows) + 1))]
    else:
        button = [*range(1, ((unique_IDs) + 1))]

    random.shuffle(button)

    print ('random : %s' % button)   # checks if list random is truly random.

for x in range(rows):

    # if matrix != unique IDs the amount of unique_IDs is created.
    if (rows * columns) != unique_IDs:                      
        button        = [*range(1, ((unique_IDs) + 1))]

        random.shuffle(button)

        print ('random : %s' % button)   # checks if list random is truly random.

    for y in range(columns):

        # number = random.choice(button) # twice the shuffle dance? Nah!
        number = button.pop(0)           # just keep popping the soda at first number 
                                         # from the randomized button order list
                                         # while it gets smaller and smaller.

        btn = tk.Button(text=number, bg="white", activebackground="black", width=2)
        btn.grid(row=y, column=x)
        i += 1
        print(' x, y, value : (%s,%s), %s' % (x, y, number))  # output check!

        # likely obsolete code below this comment line.

        if number == "1":
            row1.remove("1")
            col1.remove("1")
        elif number == "2":
            row1.remove("2")
            col1.remove("2")

... snippet ... tk GUI code here ...
© www.soinside.com 2019 - 2024. All rights reserved.