我有以下脚本:
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
。
答案 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!