May 23, 2020 04:07 AM
(This post was last modified: May 23, 2020 12:06 PM by 2xTheTap115.)
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:
ERROR:
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. 📢



![[Image: pdoOihY.png]](https://i.imgur.com/pdoOihY.png)