我有这段自称的代码:
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正确的值,但函数由于某种原因不返回该值.
可能的重复:
为什么“返回自我”返回 None?
我一直在尝试解决 Project Euler 上的问题 55 ( http://projecteuler.net/problem=55 ),现在我想我已经找到了答案,但我遇到了一个问题。我不想要问题 55 的解决方案,只想要我做错了什么。
这是我的代码:(我认为您不需要全部)
t=0
lychrel=0
called=0
def iteratepal(n):
global t
global called
called+=1
b = int(''.join(reversed(str(n))))
#print ("n =",n,"\nb =",b,"\nb+n =",b+n,"\n")
if ispal(b+n) or ispal(n):
t=0
return False
if t<50:
t+=1
iteratepal(b+n)
else: # Here's the prob
t=0 # this block is executed (because it prints "yea")
print("yea") # but it doesn't return True!
return True # you can try it yourself (even in the interpreter)
def ispal(n):
if …Run Code Online (Sandbox Code Playgroud)