Python function is returning incorrect data
Question:
The code below, when referenced in a separate file is returning incorrect data. When given data that does not match the if/else statements, it will loop through the function again, but the variable in the other file (client_type) will still be the incorrect choice.
function:
def create_client():
client_type = input()
if client_type == 'Mickey':
return 'Mickey'
elif client_type == 'Jenny':
return 'Jenny'
elif client_type == 'McElroy':
return 'McElroy'
else:
create_client()
call to the function:
client_type = functions.create_client()
if client_type == 'Mickey':
client = functions.client(3, 5, 2)
elif client_type == 'Jenny':
client = functions.client(5, 2, 3)
elif client_type == 'McElroy':
client = functions.client(4, 1, 5)
else:
print('Error on choosing client in function create_client.')
Answers:
Your problem is that when your function recurses, it returns nothing.
You should change
else:
create_client()
to
else:
return create_client()
Now, not a direct answer, but you really shouldn’t use recursion in this case, it is better with a loop:
def create_client():
while True:
client_type = input()
if client_type == 'Mickey':
return 'Mickey'
elif client_type == 'Jenny':
return 'Jenny'
elif client_type == 'McElroy':
return 'McElroy'
That won’t exhaust the recursive call stack, and saves resources.
I would even go ahead and use a dict
instead of a sequence of if
/elif
:
client_types = {
'Mickey': (3, 5, 2),
'Jenny': (5, 2, 3),
'McElroy': (4, 1, 5),
}
then you can make your code search the dict and return the correct numbers:
while True:
t = input()
if t in client_types:
break
client = functions.client(*client_types[t])
The code below, when referenced in a separate file is returning incorrect data. When given data that does not match the if/else statements, it will loop through the function again, but the variable in the other file (client_type) will still be the incorrect choice.
function:
def create_client():
client_type = input()
if client_type == 'Mickey':
return 'Mickey'
elif client_type == 'Jenny':
return 'Jenny'
elif client_type == 'McElroy':
return 'McElroy'
else:
create_client()
call to the function:
client_type = functions.create_client()
if client_type == 'Mickey':
client = functions.client(3, 5, 2)
elif client_type == 'Jenny':
client = functions.client(5, 2, 3)
elif client_type == 'McElroy':
client = functions.client(4, 1, 5)
else:
print('Error on choosing client in function create_client.')
Your problem is that when your function recurses, it returns nothing.
You should change
else:
create_client()
to
else:
return create_client()
Now, not a direct answer, but you really shouldn’t use recursion in this case, it is better with a loop:
def create_client():
while True:
client_type = input()
if client_type == 'Mickey':
return 'Mickey'
elif client_type == 'Jenny':
return 'Jenny'
elif client_type == 'McElroy':
return 'McElroy'
That won’t exhaust the recursive call stack, and saves resources.
I would even go ahead and use a dict
instead of a sequence of if
/elif
:
client_types = {
'Mickey': (3, 5, 2),
'Jenny': (5, 2, 3),
'McElroy': (4, 1, 5),
}
then you can make your code search the dict and return the correct numbers:
while True:
t = input()
if t in client_types:
break
client = functions.client(*client_types[t])