My students are making rock, paper, scissors simulations for class. One group has a bug where one of their functions won't return anything. I've checked over it to see if all branches have a return statement, and they do. I've tried a visualizer as well, and the function just stops, and I don't know why.
def Win_Loss(my_history, their_history):
if len(my_history) > 1 and len(their_history) > 1:
if (their_history[-1] == 's' and my_history[-1] == 'r'):
return 'p'
elif len(my_history) > 1 and len(their_history) > 1:
if (their_history[-1] == 'r' and my_history[-1] == 'p'):
return 's'
elif len(my_history) > 1 and len(their_history) > 1:
if (their_history[-1] == 'p' and my_history[-1] == 's'):
return 'r'
elif len(my_history) > 1 and len(their_history) > 1:
if (their_history[-1] == 's' and my_history[-1] == 'p'):
return 'r'
elif len(my_history) > 1 and len(their_history) > 1:
if (their_history[-1] == 'r' and my_history[-1] == 's'):
return 'p'
elif len(my_history) > 1 and len(their_history) > 1:
if (their_history[-1] == 'p' and my_history[-1] == 'r'):
return 's'
elif len(my_history) > 1 and len(their_history) > 1:
if (their_history[-1] == 's' and my_history[-1] == 's'):
return 'r'
elif len(my_history) > 1 and len(their_history) > 1:
if (their_history[-1] == 'r' and my_history[-1] == 'r'):
return 'p'
elif len(my_history) > 1 and len(their_history) > 1:
if (their_history[-1] == 'p' and my_history[-1] == 'p'):
return 's'
else:
return "p"
print(Win_Loss(['s','p','p'],['s','p','p']))
This should be printing 's', but it's printing None.
returnstatement. All theelifbranches have a singleifstatement. If any of thoseifs is ever not true, the function will returnNone.elif len(my_history) > 1 and len(their_history) > 1:.....Try deleting everyeliflen(my_history) > 1 and len(their_history) > 1so only the firstifblock will ever execute...