我是 Python 3.3 的新手,我开始使用套接字编写一个小猜谜游戏。但是,我不断收到错误消息ValueError: invalid literal for int() with base 10: ''
,我不明白为什么会在该行中出现此错误,currentGuess = int(currentGuess)
因为我的代码前面有类似的行。如果有人可以帮助我,我将不胜感激。这是我的服务器和客户端代码。我已经查看了具有类似问题的其他解决方案,但我无法为我工作。
服务器
#!/usr/bin/python
import random
import sys
import math
import socket
l = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
l.bind(("127.0.0.1", 4007))
l.listen(5)
print("Waiting...")
while True:
(s, ca) = l.accept()
print("Connection from", ca)
#Name
s.send("What is your name?". encode())
names = s.recv(1024).decode()
#Guesses
s.send("How many guesses would you like?".encode())
guesses = s.recv(1024).decode()
guesses = int(guesses)
#Random Number
correctNumber = random.randrange(0,10)
print(correctNumber)
#Make Guesses
count = 0
while count < guesses:
s.send("Take a Guess".encode())
currentGuess = s.recv(1024).decode()
currentGuess = int(currentGuess)
if currentGuess == correctNumber:
s.send("You WIN".encode())
if currentGuess != correctNumber:
s.send("Incorrect, Try Again ".encode())
count = count + 1
s.close()
客户
#!/usr/bin/python
import random
import sys
import math
import socket
s = socket.socket(socket.AF_INET,
socket.SOCK_STREAM)
s.connect(("127.0.0.1", 4007))
print("Wlcome to the numer guessing game")
#Name
print(s.recv(1024).decode())
name = sys.stdin.readline()
s.send(name.encode())
#guesses
print(s.recv(1024).decode())
guesses = sys.stdin.readline()
s.send(guesses.encode())
#Make Guesses
print(s.recv(1024).decode())
currentGuess = sys.stdin.readline()
s.send(currentGuess.encode())
print(s.recv(80).decode())
s.close()