else: ^ SyntaxError: invalid syntax

A forum for general discussion of the Python programming language.

else: ^ SyntaxError: invalid syntax

Postby skud123 » Fri Jul 04, 2014 10:59 am

I am a newbie to Python and I am trying to figure out where my invalid syntax is, it seems to be caused by else, I googled it some of them saying its not correctly indent and may parathesis get missed. I think all are ok, but cant....

Error:

File "c:\MyPython\Tcp\chatcserver.py", line 40
else:
^
SyntaxError: invalid syntax



Code: Select all
import socket
import os
import select
import sys

def prompt():
    sys.stdout.write('<You> ')
    sys.stdout.flush()

try:
    server_socket = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
except:
    print 'Failed to create socket'
    sys.exit()

PORT = 9050
HOST = '127.0.0.1'
RECV_BUFFER = 4096

server_socket.bind((HOST, PORT))
server_socket.listen(10)

input = [server_socket, sys.stdin]

print 'Chat Program'
prompt()

while 1:

    inputready, outputready, exceptready = select.select(input,[],[])

    for sock in inputready:

        if sock == server_socket:
            client, address = server_socket.accept()
            input.append(client)
            #data = sock.recv(RECV_BUFFER)
            #if data:
                #sys.stdout.write(data)
            else:
               data = sock.recv(RECV_BUFFER)
         if data:
                sys.stdout.write(data)
            else:
                msg = sys.stdin.readline()
                server_socket.send('\r<Server>: ' + msg)
                prompt()



server_socket.close()




Thanks,
Last edited by Yoriz on Fri Jul 04, 2014 12:15 pm, edited 1 time in total.
Reason: First post lock.
skud123
 
Posts: 2
Joined: Fri Jul 04, 2014 10:03 am

Re: else: ^ SyntaxError: invalid syntax

Postby Yoriz » Fri Jul 04, 2014 12:15 pm

Welcome to the forum, please read the new users read this link in my signature.

Either you have the previous if commented out that you want it to match up with or your need to dedent the else to bring it inline with the previous if.
Code: Select all
if sock == server_socket:
            client, address = server_socket.accept()
            input.append(client)
            #data = sock.recv(RECV_BUFFER)
            #if data: # if the following else should match this if remove the # from the start of this line
                #sys.stdout.write(data)
            else: # if you want the previous if commented out and you need it to match the one before that you need to make the indention of this line match the previous uncommented if
               data = sock.recv(RECV_BUFFER)


Looks like the last else will also have a problem with not being indented to align with an if
New Users, Read This
Join the #python-forum IRC channel on irc.freenode.net!
Spam topic disapproval technician
Windows7, Python 2.7.4., WxPython 2.9.5.0., some Python 3.3
User avatar
Yoriz
 
Posts: 842
Joined: Fri Feb 08, 2013 1:35 am
Location: UK

Re: else: ^ SyntaxError: invalid syntax

Postby skud123 » Sat Jul 05, 2014 7:24 am

Thanks yoriz...
skud123
 
Posts: 2
Joined: Fri Jul 04, 2014 10:03 am


Return to General Discussions

Who is online

Users browsing this forum: No registered users and 4 guests

cron