我有这段自称的代码:
def get_input():
my_var = input('Enter "a" or "b": ')
if my_var != "a" and my_var != "b":
print('You didn\'t type "a" or "b". Try again.')
get_input()
else:
return my_var
print('got input:', get_input())
Run Code Online (Sandbox Code Playgroud)
现在,如果我输入"a"或"b",一切都很好.输出是:
Type "a" or "b": a
got input: a
Run Code Online (Sandbox Code Playgroud)
但是,如果我输入其他内容然后输入"a"或"b",我会得到:
Type "a" or "b": purple
You didn't type "a" or "b". Try again.
Type "a" or "b": a
got input: None
Run Code Online (Sandbox Code Playgroud)
我不知道为什么get_input()要回来None,因为它应该只返回my_var.print语句显示None正确的值,但函数由于某种原因不返回该值.
我有这段代码,出于某种原因,当我尝试返回路径时,我得到的是:
def get_path(dictionary, rqfile, prefix=[]):
for filename in dictionary.keys():
path = prefix+[filename]
if not isinstance(dictionary[filename], dict):
if rqfile in str(os.path.join(*path)):
return str(os.path.join(*path))
else:
get_path(directory[filename], rqfile, path)
Run Code Online (Sandbox Code Playgroud)
有办法解决这个问题吗?提前致谢.
我正在尝试编写一个函数来检查字符串中的字符是否使用递归进行排序.这就是我想出的:
def is_sorted(x,i):
if i >= len(x):
return True
elif x[i] <= x[i-1]:
return False
else:
is_sorted(x,i+1)
Run Code Online (Sandbox Code Playgroud)
我用这些来测试我的功能:
x = "abcadef"
y = "aabcdef"
z = "abcdef"
print is_sorted(x, 1)
print is_sorted(y, 1)
print is_sorted(z, 1)
Run Code Online (Sandbox Code Playgroud)
我希望得到False,False,True,但我得到None,False,None.为什么?:(