Reputation: 1
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. 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())
Upvotes: 0
Views: 50
Reputation: 137408
Don't use recursion here. A simple while
loop is sufficient.
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()
Upvotes: 3
Reputation: 2970
You do not return a value in the "error" case. Change the x()
to return x()
Upvotes: 0