Python syntax error while taking an IP address as an input

后端 未结 2 1845
萌比男神i
萌比男神i 2021-01-29 16:31

I have a text file called sample_ips.txt containing some random IP addresses as follows:-

182.0.0.15
182.0.0.16
182.0.0.17

I am giving an IP ad

相关标签:
2条回答
  • 2021-01-29 16:40

    Well, this works:

    input = raw_input("Enter IP:")
    ip = open("sample_ips.txt", "r")
    data = ip.readlines()
    for ips in data:
        ips = ips.strip("\n")
        if input in ips:
            print "true"
        else:
            print "false"
    

    Working for me

    :edit:

    Python3 version

    input = input("Enter IP:")
    ip = open("sample_ips.txt", "r")
    data = ip.readlines()
    for ips in data:
        ips = ips.strip("\n")
        if input in ips:
            print ("true")
        else:
            print ("false")
    
    0 讨论(0)
  • 2021-01-29 16:47
    input_ip = raw_input("Enter IP:") # In python 2.x. If you use input() then type your inputs as string (>>Enter IP:"182.0.0.15")
    #input_ip = input("Enter IP:")  for python 3.x
    
    with open("ip.txt", "r") as ip:
        data = ip.readlines()
        for ips in data:
            ips = ips.strip("\n")
            if input_ip in ips:
                print ("true")
            else:
                print ("false")
    
    0 讨论(0)
提交回复
热议问题