首页 > 解决方案 > Python 2.7 的 input() 和 raw_input() 问题。用户输入的数据未正确读取。到底是怎么回事?

问题描述

所以在我的公司,他们让我使用 python 2.7 是出于兼容性的原因,我不会在这里讨论。

因此,我正在编写一个程序,该程序使用 SSH(特别是交换机)连接到设备,并且我能够使用 SSH 实际访问该设备,并且该设备可以在我的机器上 ping 通。问题 ?raw_input 似乎没有把它当作一个字符串。当我尝试 input() 时,它给了我一个无效的语法错误。

对于我编写的脚本,我通常使用 arparse 并且用户通过终端输入 IP 地址、用户名和密码,但我希望这个脚本不使用 argparse 而使用 input() 或 raw_input。我所有的 SSH 脚本都运行良好,除了这个,唯一一个使用 raw_input 和 input() 而不是 argparse

def runMain():

    scriptName = os.path.basename(__file__)

    print("The name of this script is: " + scriptName)

    print("**************************************\n")

    print("This script allows you to enable and disable ports on the SNET or SOOBM switches, have fun ! \n")

    print("**************************************\n")

    optionPrinter_switches_top()

    user_input = raw_input("Make your selection") # This does not work if I change to input(), it exits out of the program

    if user_input == 1:
        print("You selected the SNET switch, lets proceed !")

        deviceIP = input("Enter the IP address for this device")  # I tried changing these to raw_input, I get a syntax issue
        deviceUsername = input("Enter the username for this device")
        devicePassword = input("Enter the password for this device")

        confirm_ping = canPing(deviceIP) # This is a boolean function that works correctly in every script but this one.

        if confirm_ping:
            ssh_SNET = connectToSSH_SNET(deviceIP, deviceUsername, devicePassword)
        else:
           print("Sorry, that device is not even ping-able. Figure that issue out and retry the program...")
           sys.exit(-1)

        while True:
            SNET_options()

            user_choice_SNET = input("Please select an option")

            switch_result = SNET_switch_func(user_choice_SNET)

            if switch_result == "displayInterfaceBrief":
                time.sleep(5)
                displayInterfaceBrief_SNET(ssh_SNET)
            elif switch_result == "enablePort":
                time.sleep(5)
                enablePort_SNET(ssh_SNET)
            elif switch_result == "disablePort":
                disablePort_SNET(ssh_SNET)
            elif switch_result == "switchReboot":
                reboot_SNET_switch(ssh_SNET)
            else:
                print("Exiting program now....")
                sys.exit(-1)

以下是相关问题:

user_input = raw_input("Make your selection") # This does not work if I change to input(), it exits out of the program
 deviceIP = input("Enter the IP address for this device")  # I tried changing these to raw_input, I get a syntax issue
 deviceUsername = input("Enter the username for this device")
 devicePassword = input("Enter the password for this device")

confirm_ping = canPing(deviceIP) # This is a boolean function that works correctly in every script but this one.

结论 ?input()/raw_input() 存在问题。这里发生了什么,我该如何解决这个问题?我不能使用 python 3.7,这真的很令人沮丧。谢谢您的帮助

标签: pythonpython-2.7inputraw-input

解决方案


尝试更改if user_input == 1:if int(user_input) == 1:,因为输入函数默认接受字符串格式的输入。

而且,如果您想在 python 2.x 中使用input()而不是raw_input()从用户那里获取输入,那么您可以尝试以下代码:

if hasattr(__builtins__, 'raw_input'):
    input=raw_input

user_input = input("Enter a number: ")

推荐阅读