值错误:列表.删除(x) :x不在列表中错误如何修复?

2024-04-26 01:32:37 发布

您现在位置:Python中文网/ 问答频道 /正文

我有一些代码,它通过for循环,应该在tkinter窗口中提供9行9列按钮的输入和输出。这些按钮中应该有一个从1到9的随机数。但我不希望同一列和同一行中的相同数字相同。你知道吗

为了完成这一点,我尝试了.pop[]和.remove()以及del,但都没有正常工作。我得到错误row1.remove(“4”) 值错误:列表.删除(x) :x不在列表中。当我试图删除这个数字时,同一行中有两个相同的数字。有人能帮忙吗?你知道吗

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的随机数,并且行和列中的数字都不相同。你知道吗


Tags: inimportnumber列表fortkinter错误button
1条回答
网友
1楼 · 发布于 2024-04-26 01:32:37

下面带有内联代码的脚本解释了很多。因为你的矩阵是9x9,所以我使用了rows*columns=81,因为你可以把它们都标识出来。如果只有9个值,你还能得到一些双倍。易于调整unique_value。享受简单

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 ...

相关问题 更多 >