如何将值返回给下一个函数 - 不在类中

2024-09-29 23:18:18 发布

您现在位置:Python中文网/ 问答频道 /正文

我有一个2函数的代码 我想把一个函数的值返回给另一个

现在它的回报是“无”

我有两个功能: 1代表vaildip address,其他函数应获取值,并在结束到文件时使用它们:

现在validip在一个循环中工作并请求有效的ip,但是当函数完成所有4个输入时,它返回“none”作为值

def vaildIP(message):
    while True:
      answer1 = raw_input(message)
      ip = answer1.split('.')
      if (len(ip) == 4) and (1 <= int(ip[0]) <= 223) and (int(ip[0]) != 127) and (int(ip[0]) != 169 or int(ip[1]) != 254) and (
           0 <= int(ip[1]) <= 255 and 0 <= int(ip[2]) <= 255 and 0 <= int(ip[3]) <= 255):
          break
          return answer
      else:
          print "Please Enter A Vaild IP Address"
          continue





def setNetwork():
    print "[Network Interface] This step is for configuring the primary network interface (eth0) with a static IP address."
    print "[Network Interface] Please provide the new configuration values, or press Enter to use the current value."
    print "[Network Interface] Prior to saving the new values you will have the option to review them."

    address = vaildIP('Address[current value is < no - value >]:')
    netmask = vaildIP('Netmask[current value is < no - value >]:')
    gateway = vaildIP('Gateway[current value is < no - value >]:')
    print "[Network Interface] Please provide the DNS servers to use (one or more, separated with spaces), \n  or press Enter to use the existing value."
    dns = vaildIP('DNS servers [current value is <no-value>]:') or "8.8.8.8"

    print ("[Network Interface] These will be the new settings for eth0:\n" "iface eth0 inet static\n""Address {addressip} \n"
           "Netmask {netmaskip}\n" "Gateway {gatewayip}\n" "dns-nameservers {dnsip}".format(addressip=address, netmaskip=netmask, gatewayip=gateway, dnsip=dns))
    print ("Enter 'y' - to save the new configuration and load it.\n" "Enter 'n' - to re-enter the configuration details.\n" "Enter 'q' - to quit and return to main manu.")
    option = raw_input()
    if option == 'y':
        newlines = ('auto lo \n iface lo inet loopback \n # The primary network interface \n auto eth0 \n iface eth0 inet static \n address %s \n netmask %s \n gateway %s \n dns-nameservers %s ' % (
        address, netmask, gateway, dns))
        with open("/Users/yos/Desktop/net1.txt", "w+") as file:
            file.write(newlines)
    elif option == 'n':
        setNetwork()
    elif option == 'q':
        sys.exit()

if __name__ == "__main__":

    setNetwork()

期望值:

[网络接口]这些将是eth0的新设置: iface eth0 inet静态 地址1.1.1.1 网络掩码2.2.2.2 网关3.3.3.3 dns名称服务器8.8.8.8 输入“y”-保存新配置并加载它。 输入“n”-重新输入配置详细信息。 输入“q”-退出并返回主菜单。 不

实际结果: [网络接口]这些将是eth0的新设置: iface eth0 inet静态 地址无 网络掩码无 网关无 dns名称服务器8.8.8.8 输入“y”-保存新配置并加载它。 输入“n”-重新输入配置详细信息。 输入“q”-退出并返回主菜单。 不


Tags: orandthetoipvalueaddressdns

热门问题