Home > Blockchain >  ValueError: Could not convert string to a float: ' '
ValueError: Could not convert string to a float: ' '

Time:03-24

I am stuck in a very confusing problem. I try to send three integers from client to socket to solve quadratic equation. But I've try two ways and it always give me an error: ValueError: Could not convert string to a float: ' '. Please help me with this problem. Thank everyone very much. enter image description here

#Client side
import socket
import sys
HOST, PORT = "127.0.0.1", 50000
while 1 :
    # data = " ".join(sys.argv[1: ]) 
    '''
    data1 = input('Enter a : ')
    data2 = input('Enter b: ')
    data3 = input('Enter c: ')
    '''
    data = input('Enter a, b, c respectively: ')

    # create a socket ()
    with socket.socket(socket.AF_INET, socket.SOCK_STREAM) as sock :
        #
        sock.connect((HOST, PORT))
        #sock.send(str.encode("\n".join([data1, data2, data3])))
        sock.send(str(data).encode())
        #
        received = sock.recv(1024)

        received = received.decode('utf-8')
        received = eval(received)
        #print('Sent: ', data1, ' ', data2, ' ', data3)
        print('Sent: ', data)
        print('Received: ', received)
        break
# Server side
from encodings import utf_8
import math
from socket import socket, AF_INET, SOCK_STREAM

from sympy import re
BUFFER_SIZE = 1024
s = socket(AF_INET, SOCK_STREAM)
s.bind(("", 50000))
s.listen(5)
print('Server is listening...')
conn, addr = s.accept()

def GiaiPhuongTrinhBac2(a, b, c) :
    lst = []
    if a == 0 :
        if b == 0 :
            if c == 0 :
                return lst.append('Phương trình vô số nghiệm!')
            else : 
                return lst.append('Phương trình vô nghiệm!')
        else :
            x = -(c / b)
            return lst.append(x)
    else :
        delta = b * b - 4 * a * c
        if delta == 0 :
            return lst.append( (-b) / (2 * a) ) 
        elif delta > 0 :
            x1 = (-b - math.sqrt(delta)) / (2 * a)
            x2 = (-b   math.sqrt(delta)) / (2 * a)
            lst.append(x1)
            lst.append(x2)
        else :
            lst.append('Phương trình vô nghiệm!')

while 1 :
    '''
    received = conn.recv(BUFFER_SIZE).decode('utf-8').split("\n")

    data1 = float(received[0])
    data2 = float(received[1])
    data3 = float(received[2])

    if not (data1 and data2 and data3) :
        break
    print('Received data: ', data1, data2, data3)
    '''
    received = conn.recv(BUFFER_SIZE).decode('utf-8').strip().split(" ")
    print(received)
    data1 = float(received[0])
    data2 = float(received[1])
    data3 = float(received[2])

    lst =[]
    lst = GiaiPhuongTrinhBac2(data1, data2, data3)
    '''
    lst = []
    lst.append(received)
    '''
    conn.send(str(lst).encode())

conn.close()

I tried to enter a string of a, b, c to split with ' ' and '\n' but it seems unsuccessful.

CodePudding user response:

After send, the received data is flushed. So the next round in while loop received variable data is null. So you, don't use while loop or move these lines in to the while loop:

while True:
   s.listen(5)
   conn, addr = s.accept()

And in your code have more problem, for example you can't append in return. The right solution:

lst.append('Phương trình vô số nghiệm!')
return lst

CodePudding user response:

I ran your code and everthing works just fine. You have to make sure that you put some spaces beetween your input or format it the right way, like so: "1 2 3". Then everthing sould work.

  • Related