我正在尝试使用 python 中的 for 循环创建列表列表。我的计划是使用两个单独的循环;一个循环创建大列表以放入小列表,另一个循环创建小列表并将它们附加到大列表。
这被用来制作“战舰”类型的游戏,其中有 10x10 网格。
这是我遇到困难的代码块:
for i in range(0,10):
(newlist,(i))=[]
Run Code Online (Sandbox Code Playgroud)
该特定代码要完成的唯一任务是创建 10 个新列表,每个列表都有不同的名称。例如,循环的第一个循环将创建list0,第二个循环将创建list1,依此类推,直到list9。
从理论上讲,我认为这段代码没有任何问题。当您将字符串放入新变量而不是创建新列表时,它也可以工作。
每当我运行该程序时,我都会收到错误
ValueError: not enough values to unpack (expected 2, got 0)
Run Code Online (Sandbox Code Playgroud)
我不知道为什么会发生这种情况,如果这里有人能帮助我,我将非常感激。
我正在 Pygame 中创建一个游戏,需要多人游戏的客户端服务器部分。
首先,我检查连接是否少于两个。如果是这种情况,客户端将看到一个屏幕,显示“正在等待连接”。
我已经让客户端成功向服务器发送“1”消息,如果服务器未满,服务器将响应“1”。因此,如果服务器未响应 1,则服务器已满,客户端可以继续。
但是,我收到标题中提到的此错误。
服务器代码:
import socket
import sys
import threading
from _thread import *
import time
s=socket.socket(socket.AF_INET, socket.SOCK_STREAM)
host=socket.gethostname()
ip=socket.gethostbyname(host)
port=8000
connections=[]
print('Your local ip address is',ip)
s.bind((host,port))
s.listen(2)
def threaded_client(connection):
while True:
data=connection.recv(2048) #anything we receive
if not data:
break
connection.close()
def checkconnected(connections):
noofconn=len(connections)
while True:
print('Waiting for a connection...')
connection,address=s.accept()
print(address,'has connected to server hosted at port',address[1])
connections.append(address)
data=connection.recv(1024)
received=[]
counter=0
for letter in data:
received.append(data[counter])
counter+=1
received=(chr(received[0]))
if received=='1':#handling initial connections
if len(connections)!=2: …Run Code Online (Sandbox Code Playgroud)