Python Forum
Math operations from string in a Client-Server over UDP
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Math operations from string in a Client-Server over UDP
#1
A user is to input an expression, such as "5+5", this will be sent to the server where it will be calculated and sent back to the client. This goes on until the terminator "***" is entered. My question is how do I extract the numbers and the operators from the string in order to calculate and give an answer?

My Client code:
from socket import *
serverName = localhost'
serverPort = 12000

clientSocket = socket(AF_INET, SOCK_DGRAM)

message = input("Input expression: ")


clientSocket.sendto(bytes(message,"utf-8"),  (serverName, serverPort))

modifiedMessage, serverAddress = clientSocket.recvfrom(1024)

print ("Received: ", modifiedMessage.decode())

clientSocket.close()
My Server code:
from socket import*

serverPort = 12000
serverSocket = socket(AF_INET, SOCK_DGRAM)
serverSocket.bind(("",serverPort))

print ("Ready")
while 1:
    message, clientAddress = serverSocket.recvfrom(1024)

    print("Expression: ",message)

    for j in range(0,len(message)):
        num1=message[j]
        op=message[j+1]
        num2=message[j+2]
    if op=='+':
        modifiedMessage= num1+num2

    serverSocket.sendto(modifiedMessage, clientAddress)
    print("Sent: ",modifiedMessage)
This gives no output, what am I doing wrong
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Need help implementing an input with file operations Gaijin 3 2,086 Jun-08-2022, 05:50 PM
Last Post: Gaijin
  Order of operations for reassignment of a variable StillAnotherDave 1 1,697 Jan-21-2020, 12:20 PM
Last Post: buran
  I'm a bit confused with these boolean operations on integers goelraghavg 6 3,659 Aug-25-2018, 03:16 PM
Last Post: perfringo
  Arithmetic operations using lists yassine 2 2,397 May-02-2018, 06:20 PM
Last Post: j.crater
  Upload file to server from client oncebuddy 6 5,938 Feb-15-2018, 03:23 PM
Last Post: buran

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020