Hosein Azimi
Hosein Azimi

Reputation: 1

Can't add string to a string type variable in Python

I wanted to make a very simple network scanner in python, and I wrote this:

import subprocess
ip_range = input("Enter ip range:")
start_ip = input("Enter start ip:")
end_ip = input("Enter end ip:")
result = "Result:"
for ping in range(int(start_ip), int(end_ip)):
    ip = ip_range + str(ping)
    connect = subprocess.call(["ping", "-c1",ip])
    if connect == 0:
        result += "---"
        result += "Ping to ",ip,"was OK"
        result += "---"
    else:
        result + "Couldn't ping to " + str(ip)
        result + "---"
        pass
print(result)

All of them work correct except one thing. In this part

result + "Couldn't ping to " + str(ip)
result + "---"

The result doesn't add up to this code, I mean it just write result.

Upvotes: 0

Views: 424

Answers (1)

Josh Clark
Josh Clark

Reputation: 1012

result + "Couldn't ping to " + str(ip) is an expression, not a statement. You don't assign that value to result. Try changing the + to +=. That will store the value result + "Couldn't ping to " + str(ip) in result.

Upvotes: 1

Related Questions