我不知道为什么我的删除和ping不会工作。蟒

时间:2015-12-22 16:26:19

标签: python

import platform
import subprocess
import webbrowser

ipAddress = []

def show_ip():
    i = -1
    if(len(ipAddress) ==0):
        print('There are no IPs assigned')
        print ("")
    else:
        print("----List of IP Addresses----")
        for z in ipAddress:
            i = i + 1
            print("IP (", i, "):" , z)
        print("")

def add_ip():
    addIP = input('Enter IP: ')
    ipAddress.append(addIP)
    print('IP Added')
    print('')

def update_ip():
    updateIP = int(input('Enter IP [index] to update: '))
    newIP = input('Enter new IP: ')
    ipAddress[updateIP] = newIP
    print('Ip Updated')
    print('')

def delete_ip():
    delIP = input('Enter IP Address to Delete: ')
    ipAddress.remove(delIP)
    print ('IP', delIP, 'Deleted')
    print('')

def ping_ip():
    pingAddress = ""
    for b in ipAddress:
        if(platform.system() == 'Windows'):
            pingAddress = '-n'
        else:
            pingAddress = '-c'

        status = subprocess.call(['ping', pingAddress, '1', '127.0.0.1'])
        if status == 0:
            print (b, "Ping Successful")
        elif status == 2:
            print(b, 'Ping No respnse')
        else:
            print (b, 'Ping Failed')
    print('')

while(True):

        print ('------MENU------')
        print('[1] - Show Stored IPs')
        print('[2] - Add IP Address')
        print('[3] - Update IP Address')
        print('[4] - Delete IP Address')
        print('[5] - Ping IP Address')
        print('[6] - Help') 
        print('[7] - Exit ')

        choice = input('Please enter your choice: ')
        print('')

        if (choice =='1'):
            show_ip()
        elif (choice =='2'):
            add_ip()
        elif (choice =='3'):
            update_ip()
        elif (choice =='4'):
            delete_ip
        elif (choice =='5'):
            ping_ip
        elif (choice =='6') :
            webbrowser.open('help.html')    
        elif (choice =='7'):
            exit()
        else:
            print('Invalid input')

我不知道为什么选项4和5不会工作。它只是在我做的菜单中一次又一次地循环。有人可以帮助我,因为我很难搞清楚它为什么不起作用。如果有人弄明白,我会非常感激,因为我尝试了4个多小时重新安排代码才能让它工作

2 个答案:

答案 0 :(得分:3)

您在delete_ip和ping_ip之后忘记了括号()。

答案 1 :(得分:1)

你没有调用函数:

elif (choice =='4'):
    delete_ip
elif (choice =='5'):
    ping_ip

尝试:

elif (choice =='4'):
    delete_ip()
elif (choice =='5'):
    ping_ip()