[英]I don't know why my delete and ping wont work. 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 多個小時重新安排代碼以使其正常工作
你忘記了 delete_ip 和 ping_ip 后面的括號 ()。
您不是在調用函數:
elif (choice =='4'):
delete_ip
elif (choice =='5'):
ping_ip
嘗試:
elif (choice =='4'):
delete_ip()
elif (choice =='5'):
ping_ip()
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.