在python3中预期缩进的块

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

这是老师给我们的用于家庭作业的代码。应该不会有错误。但是还有。请帮我修复它。 (错误在第9行)

import socket

serverPort = 12000
serverSocket = socket.socket(socket.AF_INET,socket.SOCK_STREAM)
serverSocket.bind(('',serverPort))
serverSocket.listen(1)
print ("The server is ready to receive")
while 1:
connectionSocket, addr = serverSocket.accept()
byte_sentence = connectionSocket.recv(1024)
utf_sentence = byte_sentence.decode("utf-8")
modified_utf_sentence = utf_sentence.upper()
modified_byte_sentence = bytes(modified_utf_sentence, "utf-8")
connectionSocket.send(modified_byte_sentence)
connectionSocket.close()

文件“ tcpserver.py”,第9行connectionSocket,addr = serverSocket.accept()^IndentationError:预期为缩进的块

python-3.7
1个回答
0
投票

声明while循环后,您需要将代码缩进4个空格。

import socket

serverPort = 12000
serverSocket = socket.socket(socket.AF_INET,socket.SOCK_STREAM)
serverSocket.bind(('',serverPort))
serverSocket.listen(1)
print ("The server is ready to receive")
while 1:
    connectionSocket, addr = serverSocket.accept()
    byte_sentence = connectionSocket.recv(1024)
    utf_sentence = byte_sentence.decode("utf-8")
    modified_utf_sentence = utf_sentence.upper()
    modified_byte_sentence = bytes(modified_utf_sentence, "utf-8")

connectionSocket.send(modified_byte_sentence)
connectionSocket.close()

while循环中的所有内容都必须缩进4个空格。返回未缩进的代码将终止while循环。

© www.soinside.com 2019 - 2024. All rights reserved.