Home Upgrade Search Memberlist Extras Hacker Tools Award Goals Help Wiki Contact

Python 

[SOLVED]Invalid syntax in the elif clause

I warmly welcome🙂
For some time I've been learning the Python language and I'm trying to write a simple program to check the reachability of a given host on the network. However, I am stuck on the elif clause in which it is to check the operating system on which the script is run. Every time the invalid syntax error crashes although it seems to me that the syntax is correct (i.e. when it goes to the second condition which should return true because I use linux it is then that the error crashes). The python interpreter points to the wrong beginning of the elif clause. I admit that I have no idea what I'm doing wrong🙄 Any of you have an idea how to fix this? Below is the program code and error message.
Thank you in advance for your help and best regards👍

PROGRAM:
Code
# -*- coding: utf-8 -*-

import sys
import subprocess

def ping(address, count=1):
cmd = ['ping']

if sys.platform in ('windows', 'win32'):
cmd.append('-n')
print("Platforma: Windows")

elif sys.platform.startswith('linux'):
cmd.append('-c')
print("Platforma: Linux")

elif sys.platform.startswith('freebsd'):
cmd.append('-c')
print("Platforma: FreeBSD")

else sys.platform.startswitch('darwin'):
cmd.append('-c')
print("Platforma: MacosX")

cmd.append(str(count))
cmd.append(address)

ping_process = subprocess.Popen(cmd, stdout=subprocess.PIPE)
ping_process.wait()

return ping_process.returncode == 0

if __name__ == '__main__':
  addresses = ['www.google.pl']

  for address in addresses:
      if ping(address):
          print('Host %r jest osgiągalny' % address)
      else:
          print('Host %r jest nie osiągalny' % address)

ERROR:

Code
python3 Alarm.py                                                1 ↵
 File "Alarm.py", line 13
   elif sys.platform.startswith('linux'):
   ^
SyntaxError: invalid syntax
This place could be yours, click here for details. 📢
Seems like you're not indenting following your conditional statements. Follow PEP 8. Although it maybe just how you pasted it here but check to make sure you indented properly. It looks like you indented properly though at the bottom of the code.
He who cannot obey himself will be commanded - F. Nietzsche
(May 23, 2020 10:15 AM)Mesmaroth Wrote:  Seems like you're not indenting following your conditional statements. Follow PEP 8. Although it maybe just how you pasted it here but check to make sure you indented properly. It looks like you indented properly though at the bottom of the code.

Thank you very much for the tips, in a moment I will check all the code in this regard🙂👍 I always try to use TAB as an indent (I know that many use quadruple spaces, but then it's easy to make a mistake) I only wonder this arrow in the error code that indicates the beginning of the elif clause. But as I look at it now, you may be right that I failed my indentation
This place could be yours, click here for details. 📢
(May 23, 2020 10:53 AM)2xTheTap115 Wrote:  Thank you very much for the tips, in a moment I will check all the code in this regard🙂👍 I always try to use TAB as an indent (I know that many use quadruple spaces, but then it's easy to make a mistake) I only wonder this arrow in theerror code that indicates the beginning of the elif clause. But as I look at it now, you may be right that I failed my indentation

EDITED: I checked and it turns out that the indentation is fine (and you were right in the post it looks completely different than in my editor which can give a wrong impression). For now, I'm going back to the starting point

Can you repost your code with correct indentation?
It would be easier for us that way.
[Image: pdoOihY.png]
(May 23, 2020 10:15 AM)Mesmaroth Wrote:  Seems like you're not indenting following your conditional statements. Follow PEP 8. Although it maybe just how you pasted it here but check to make sure you indented properly. It looks like you indented properly though at the bottom of the code.

(May 23, 2020 11:49 AM)Athr Wrote:  Can you repost your code with correct indentation?
It would be easier for us that way.


There will be no such need. Colleague Mesmaroth was right (with you too) The whole problem was in my oversight. I didn't notice that the elif block contained one extra space at the beginning of the line that caused the indentation to be wrong🤦‍♂️🤦‍♂️🤦‍♂️. I don't know how I could have missed it, but it's obviously my fault. Despite everything, thank you for your help in this topic🙂👍
This place could be yours, click here for details. 📢
(May 23, 2020 12:02 PM)2xTheTap115 Wrote:  There will be no such need. Colleague Mesmaroth was right (with you too) The whole problem was in my oversight. I didn't notice that the elif block contained one extra space at the beginning of the line that caused the indentation to be wrong🤦‍♂️🤦‍♂️🤦‍♂️. I don't know how I could have missed it, but it's obviously my fault. Despite everything, thank you for your help in this topic🙂👍

No worries. Happens to the best of us.
[Image: pdoOihY.png]


Possibly Related Threads...
Thread / Author Replies Views Last Post
[Python] by ZakMay 2, 2018 05:26 AM 2 116 May 2, 2018 05:26 AM
Last Post: F.A.S.T
[C] by datguy.devJan 6, 2018 02:35 PM 0 132 Jan 6, 2018 02:35 PM
Last Post: datguy.dev
[Python] by M O D E R NDec 13, 2017 04:14 PM 2 168 Dec 13, 2017 04:14 PM
Last Post: M O D E R N