函数返回无

时间:2017-03-03 19:37:32

标签: python python-2.7

我有以下脚本:

def collectUserPrompt():    

    xml_file = raw_input('Enter the full file path (ex: C:\Users\Stan\Documents\myxml.xml):')

    try:
        os.path.isfile(xml_file)
        print 'good path'
    except:
        print "Please enter a valid xml file path!"
        collectUserPrompt()

    multiplier_input = raw_input('Enter XML multiplier (numbers only):')
    try: 
        multiplier = int(multiplier_input)
        print 'good multiplier'
    except :
        print "Please enter a valid integer!"
        collectUserPrompt()

        return xml_file, multiplier   

user_input = collectUserPrompt()
print user_input

当我跑步时,我得到:

Enter the full file path (ex: C:\Users\LIDMZ3W\Documents\myxml.xml):C:\Users\Stan\Documents\Aptana Studio 3 Workspace\ADFTest\src\Training\nascoUMBY.xml
good path
Enter XML multiplier (numbers only):3
good multiplier
None

当我尝试从None函数传回两个经过验证的用户输入时,我似乎每次都会返回raw_input

1 个答案:

答案 0 :(得分:2)

您的return声明位于except块中。好像你的意思是直接在函数块下面。即,你需要在缩进时将其恢复一级:

def collectUserPrompt():    

    xml_file = raw_input('Enter the full file path (ex: C:\Users\Stan\Documents\myxml.xml):')

    try:
        os.path.isfile(xml_file)
        print 'good path'
    except:
        print "Please enter a valid xml file path!"
        collectUserPrompt()

    multiplier_input = raw_input('Enter XML multiplier (numbers only):')
    try: 
        multiplier = int(multiplier_input)
        print 'good multiplier'
    except :
        print "Please enter a valid integer!"
        collectUserPrompt()

    return xml_file, multiplier # here!