python 2.7 - Lines in my code being skipped and I am not sure why -
i using python 2.7 , pretty new python. wanted ask why lines in code being skipped although don't see reason them be.
my code seen below:
def add_client: code adding client def check_clients: code listing out client info modes = {'add': add_client, 'check': check_clients} while true: while true: action = raw_input('input action: \n').lower() if action in modes or ['exit']: break print 'actions available:', in modes: print i.title() + ',', print 'exit' if action in modes: modes[mode](wb) if action == 'exit': break
when run code , input action not in list of modes, not print out 'actions available: add, check, exit' , seems skip seen below.
input action: k input action:
if change code seen below works intended:
modes = {'add': add_entries, 'check': check_stats} while true: while true: mode = raw_input('input action: \n').lower() if mode not in modes: print 'actions available:', in modes: print i.title() + ',', print 'end/exit' if mode in modes or ['end', 'exit']: break if mode in modes: modes[mode](wb) if mode in ['end', 'exit']: break
output:
input action: k actions available: add, check, end/exit
from understanding, thought when if statement false, code within if statement skipped , code following should ran, reason, doesn't seem case here. there reason or understanding of if statement incorrect?
the condition action in modes or ['exit']
evaluates true
regardless of value of action
. read (action in modes) or (['exit'])
(so apply or
operator operands action in modes
, ['exit']
). non-empty list ['exit']
evaluates true
in boolean context, or
returns true
. suggested use action in modes or action == 'exit'
here achieve goal.
Comments
Post a Comment