[英]Functions calling functions
The problem with this code is that if you input anything other than "bob"
first, when you finally input "bob"
, the main function will print None
instead. 此代码的问题在于,如果您首先输入
"bob"
以外的任何内容,那么当您最终输入"bob"
,主功能将改为打印None
。 Please run this code to fully understand what i'm having trouble with and to provide me some answers. 请运行此代码以完全了解我遇到的问题并提供一些答案。
def main(name):
print name
def x():
name = raw_input()
if name == "bob":
return name
else:
print "error"
x()
main(x())
Don't use recursion here. 不要在这里使用递归。 A simple
while
loop is sufficient. 一个简单的
while
循环就足够了。
def get_name_must_be_bob():
while True:
name = raw_input("Enter name: ")
if name.lower() == "bob": # "Bob", "BOB" also work...
return name
# `else` is not necessary, because the body of the `if` ended in `return`
# (we can only get here if name is not Bob)
print "Are you sure you're not Bob? Try again."
def main():
name = get_name_must_be_bob()
print "Hello, " + name
if __name__ == '__main__':
main()
You do not return a value in the "error" case. 在“错误”情况下,您不会返回任何值。 Change the
x()
to return x()
更改
x()
以return x()
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.