UDP Calculator syntax error

General discussions and questions abound development of code with MicroPython that is not hardware specific.
Target audience: MicroPython Users.
Post Reply
promocode12
Posts: 1
Joined: Tue Oct 20, 2020 1:25 pm

UDP Calculator syntax error

Post by promocode12 » Tue Oct 20, 2020 1:30 pm

Not sure what the error is because I checked some other code and it looks about right

Code: Select all

>>> from socket import *

>>> serverName= 'Localhost'
>>> serverPort=12345

>>> serverSocket=socket(AF_INET,SOCK_DGRAM)
>>> serverSocket.bind(("",serverPort))
>>> print("Calculating expressions..")
Calculating expressions..

>>> file2.open("output,txt","w")
while 1:
	exp,clientAddress=serverSocket.recvfrom(2048)
	exp=exp.decode("utf-8")

	print("Infix Expr    Value")
	print("----------------------")

	file2.write("Infix Expr     Value")
	file2.write("---------------------")

	for x in range(0,len(exp)):
		if exp[x] >= "0" and exp[x]<="9":
			list.append(exp[x])
		elif exp[x]=="+"or exp[x]=="-" or exp[x]=="/" or exp[x]=="*":
			op=exp[x]
           
	list.append(op)
	temp=list.pop()
     
	if temp=="+":
		num2=int(list.pop())
		num1=int(list.pop())
		ans=num1+num2
	
	elif temp=="-":
		num2=int(list.pop())
		num1=int(list.pop())
		ans=num1-num2
		
	elif temp=="*":
		num2=int(list.pop())
		num1=int(list.pop())
		ans=num1*num2

	elif temp=="/":
		num2=int(list.pop())
		num1=int(list.pop())
		ans=int(num1/num2)


	print(exp,"  =  ",ans)
	ans=str(ans)
	serverSocket.sendto(bytes (ans,"utf-8"),clientAddress)

	file2.write(ans)
	file2.write("\n")
	
print("-----------------------")
file2.write("---------------------")
file2.close()
    #print("Send answer of: ",ans,"to client")
    
serverSocket.close()

Post Reply