http://docs.python.jp/3/tutorial/controlflow.html#default-argument-values I was curious about the code that used the in keyword.
I tried the following code.
http://ideone.com/kOEGWg
inp = 'ten'
if inp in 'tensor':
print('include1')
if inp in 'ten':
print('include2')
if inp in 'te':
print('include3')
result
Success time: 0.01 memory: 9992 signal:0
include1
include2
It seems to be True at the time of partial match.
Related http://www.pythonweb.jp/tutorial/list/index10.html
However, if the partial match is True, I feel that the following ʻif ok in ('y','ye','yes'): can be ʻif ok in'yes':
. I confirmed it with ideone.
def ask_ok(prompt, retries=4, reminder='Please try again!'):
while True:
ok = input(prompt)
if ok in ('y', 'ye', 'yes'):
return True
if ok in ('n', 'no', 'nop', 'nope'):
return False
retries = retries - 1
if retries < 0:
raise ValueError('invalid user response')
print(reminder)
Recommended Posts