我正在尝试使用简单的客户端和服务器使用python编写a子手游戏。我先打开服务器并在套接字上侦听,然后让客户端连接到服务器并发送消息,然后服务器用由_ *表示的秘密单词(由秘密单词的字母数表示)响应。
我写了一个不使用套接字的子手游戏,所以我知道,但是问题是我无法使If语句正常工作。他们要么不返回任何东西,要么带来语法错误。这是没有if语句的服务器
import socket
import random
#Characters and places from HHGTTG to make it a little more difficult
word_list = [ 'agrajag','altairians','android','apple','arthur','beeblebrox',
'betelgeuse','colin','dent','dentrassis','dolphins','eddie','fenchurch',
'ford','galaxy','gargravarr','garkbit','god','golgafrinchans','guide',
'hactar','harmless','hello','hitchhikers','hooloovoo','hyperspace',
'krikkiters','lallafa','laminate','lamuella','magrathea','marvin',
'panic','paranoid','prefect','random','roosta','russell',
'slartibartfast','sorcerer','thor','towel','trillian','vogon','willow',
'zaphod','zarniwoop','zarquon','zem']
lowercase = ['q','w','e','r','t','y','u','i','o','p','l','k','j','h','g','f',
'd','s','a','z','x','c','v','b','n','m']
secret_word = random.choice(word_list)
guesses=0
letters_guessed = []
word = []
#while testing i used this a lot and i have left it in in case you want to cheat as well
#print(secret_word)
for x in range(len(secret_word)):
word.append('_ ')
arthur = ("\n %s"%''.join(word))
def Main():
host = ''
port = 4242
s = socket.socket()
s.bind((host,port))
s.listen(1)
c, addr = s.accept()
print("Connection from: " + str(addr), secret_word)
while True:
data = c.recv(1024).decode('utf-8')
if not data:
break
print("from connected user: " + data)
data = data.upper()
print("sending: " + arthur)
c.send(arthur.encode('utf-8'))
c.close()
if __name__ == '__main__':
Main()
if语句引起如此多的头痛是这个
while True:
data = c.recv(1024).decode('utf-8')
if len(data) == 1:
print ("BlaBlaBla")
else:
print ("Bohoohooho")
显然,这不是我将在游戏中使用的if和else语句,但这是我的测试,并且不起作用。它不断提出我检查的语法错误,然后
我使用的是gedit,设置有误,因此它添加了制表符而不是空格,因此尽管看起来正确,但无法正常工作。只是将其更改为添加空格即可正常工作,不确定是否会发生这种情况,甚至不确定是否发生了这种情况,但我仍然会尽其所能。
谢谢
本文收集自互联网,转载请注明来源。
如有侵权,请联系 [email protected] 删除。
我来说两句