[英]missing 1 required positional argument?
我不明白這段代碼有什么問題? 它一直給我一個錯誤。 我不想創建 class。 它一直在給我“在主 function 中缺少 1 個必需的位置參數'選擇'。有人有什么建議嗎?腳本應該是一個菜單,所有功能都連接到主。我試着做 elif,希望它有所幫助。我可能需要使用“自我”
import socket
import uuid
import os
import re
HNAME=1
IP=2
MAC=3
ARP=4
ROUT=5
QUIT=6
def get_user_choice(choice):
print("Network Toolkit Menu")
print("_____________________________________")
print("1. Display the Host Name")
print("2. Display the IP Address")
print("3. Display the MAC Address")
print("4. Display the ARP Table")
print("5. Display the Routing Table")
print("6. Quit")
print()
choice = int(input("Enter your choice: "))
print()
return choice
def choicefun(choice):
while choice > QUIT or choice < HNAME:
choice = int(input("Please enter a valid number: "))
print()
return choice
def get_hostname(host):
host=socket.gethostname()
print("\n The host name is: ", host)
#return host
def get_ipaddr(ipv4):
ipv4=socket.gethostbyname()
print("\n The IPv4 address of the system is: ", ipv4)
#return ipv4
def get_mac(ele):
print ("The MAC address is : ", end="")
print (':'.join(['{:02x}'.format((uuid.getnode() >> ele) & 0xff)
for ele in range(0,8*6,8)][::-1]))
def get_arp(line):
print("ARP Table")
with os.popen('arp -a') as f:
data=f.read()
for line in re.findall('([-.0-9]+)\s+([-0-9a-f]{17})\s+(\w+)',data):
print(line)
return line
def __pyroute2_get_host_by_ip(ip):
print("Routing table\n: ")
table=os.popen('route table')
print(table)
def main(choice):
counter=False
while counter==False:
get_user_choice()
choicefun()
if choice == 6:
counter==True
elif choice == 1:
get_hostname()
elif choice == 2:
get_ipaddr()
elif choice == 3:
get_mac()
elif choice== 4:
get_arp()
elif choice == 5:
__pyroute2_get_host_by_ip()
main()
發生這種情況是因為您在沒有相應的choice
參數的情況下調用main
function:
def main(choice):
...
main()
您要么需要傳遞choice
參數,要么從 function 中刪除choice
參數。 似乎choice
主要由get_user_choice()
定義,在這種情況下,代碼可以讀取:
def main():
counter=False
while counter==False:
choice = get_user_choice()
...
但是, get_user_choice
function 也有一個choice
參數。 由於此參數被choice = int(input("Enter your choice: "))
覆蓋,因此您可能希望將 function 定義為:
def get_user_choice():
...
這個靈魂的工作:
import socket
import uuid
import os
import re
HNAME=1
IP=2
MAC=3
ARP=4
ROUT=5
QUIT=6
def get_user_choice():
print("Network Toolkit Menu")
print("_____________________________________")
print("1. Display the Host Name")
print("2. Display the IP Address")
print("3. Display the MAC Address")
print("4. Display the ARP Table")
print("5. Display the Routing Table")
print("6. Quit")
print()
choice = int(input("Enter your choice: "))
return choice
def choicefun(choice):
while choice > QUIT or choice < HNAME:
choice = int(input("Please enter a valid number: "))
print()
return choice
def get_hostname(host):
host=socket.gethostname()
print("\n The host name is: ", host)
#return host
def get_ipaddr(ipv4):
ipv4=socket.gethostbyname()
print("\n The IPv4 address of the system is: ", ipv4)
#return ipv4
def get_mac(ele):
print ("The MAC address is : ", end="")
print (':'.join(['{:02x}'.format((uuid.getnode() >> ele) & 0xff)
for ele in range(0,8*6,8)][::-1]))
def get_arp(line):
print("ARP Table")
with os.popen('arp -a') as f:
data=f.read()
for line in re.findall('([-.0-9]+)\s+([-0-9a-f]{17})\s+(\w+)',data):
print(line)
return line
def __pyroute2_get_host_by_ip(ip):
print("Routing table\n: ")
table=os.popen('route table')
print(table)
def main():
counter=False
while counter==False:
choice = get_user_choice()
choicefun(choice)
if str(choice) == "6":
counter==True
elif str(choice) == "1":
get_hostname()
elif str(choice) == "2":
get_ipaddr()
elif str(choice) == "3":
get_mac()
elif str(choice)== "4":
get_arp()
elif str(choice) == "5":
__pyroute2_get_host_by_ip()
main()
您的問題是您沒有通過所需的 arguments 來插入 function。
問題很簡單:當您將其定義為需要它時,您調用主 function 而沒有 arguments。
def main(choice):
...
main() #<-here
您需要傳遞一個值作為您的初始選擇或從main
定義中刪除它
選項 1 調用它,例如
main(1)
選項 2
def main():
...
但這會給您帶來第二個問題,在您的主要 function 中,您永遠不會在這里分配或更改choice
變量從get_user_choice
獲得的值,因此您將收到第二個錯誤,具體取決於您如何解決第一個問題,或陷入無限循環,因為選擇不會改變
get_user_choice(choice)
您錯過了將選項作為 arg 傳遞
我認為你應該重新評估你的設計:
def get_user_choice(choice):
print("Network Toolkit Menu")
選擇未設置,因此傳遞尚未創建的參數是沒有用的。
當您創建像您這樣的腳本時,Python 是一種過程和動態編程語言(即使對此有很多爭論也接受它),從第一行到最后一行執行您的代碼。 那將是為了
import socket import uuid import os import re HNAME=1 IP=2 MAC=3 ARP=4 ROUT=5 QUIT=6 def get_user_choice(): print("Network Toolkit Menu") print("_____________________________________") print("1. Display the Host Name") print("2. Display the IP Address") print("3. Display the MAC Address") print("4. Display the ARP Table") print("5. Display the Routing Table") print("6. Quit") print()#??? choice = int(input("Enter your choice: ")) print("Your Choice is {0}".format(choice)) return choice def choicefun(choice): while choice > QUIT or choice < HNAME: choice = int(input("Please enter a valid number: ")) print()#Wtf? return choice def get_hostname(host): host=socket.gethostname() print("\n The host name is: ", host) #return host def get_ipaddr(ipv4): ipv4=socket.gethostbyname() print("\n The IPv4 address of the system is: ", ipv4) #return ipv4 def get_mac(ele): print ("The MAC address is: ", end="") print (':'.join(['{:02x}'.format((uuid.getnode() >> ele) & 0xff) for ele in range(0,8*6,8)][::-1])) def get_arp(line): print("ARP Table") with os.popen('arp -a') as f: data=f.read() for line in re.findall('([-.0-9]+)\s+([-0-9a-f]{17})\s+(\w+)',data): print(line) return line def __pyroute2_get_host_by_ip(ip): print("Routing table\n: ") table=os.popen('route table') print(table) def main(): counter=False #while:counter: while counter==False: choice = get_user_choice() choicefun(choice)#you forgot to pass choice parameter if choice == 6: counter==True elif choice == 1: get_hostname() elif choice == 2: get_ipaddr() elif choice == 3: get_mac()#you need to return something and save it in a variable elif choice== 4: get_arp()#same elif choice == 5? __pyroute2_get_host_by_ip(#?:)#you need to pass the parameter ip if __name__ == 'main': main()
我修正了一點:如果你是初學者並且沒有太多經驗,記得寫輸入,output和function的目的。 第 2 點,如果調用 function 名稱中帶有“get”的 function,則必須執行variableOfReturn = getMyfunction(parameter1,paramater2...)
。
第 3 點,始終檢查 function 的聲明,如果它需要參數,則必須以兩種方式傳遞它:使用已設置的變量或靜態變量,如choicefun(2)
。
沒有必要創建一個 class,如果你想有一個中央 function 調用其他 function 就像一個菜單,使用if __name__ == '__main__':
編碼 101 有一些嚴重不足,請注意,您最好嘗試使用 codechef 或其他一些系統。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.