私はこのコードを持っています:
def time24hr(tstr):
if ('a' and ':') in tstr:
if tstr[:2] == '12':
tstr = tstr.replace('12', '00').replace(':','').replace('am','hr')
return tstr
elif len(tstr[:tstr.find(':')]) < 2:
# If length of string's 'tstr' slice (that ends before string ':') is less than 2
tstr = tstr.replace(tstr[:tstr.find(':')],'0' + tstr[:tstr.find(':')]).replace(':', '').replace('am','hr')
# Replace one-character string with combination of '0' and this one-character string. Then remove colon, by replacing it with lack of characters and replace 'am' string with 'hr'.
return tstr
else:
tstr = tstr.replace(':', '').replace('am', 'hr')
return tstr
elif ('p' and ':') in tstr:
tstr = tstr.replace(':', '').replace('pm', 'hr')
return tstr
else:
return "Time format unknown. Type correct time."
このコードを実行すると、文字列print time24hr('12:34am')
が返されます。0034hr
これについても機能しprint time24hr('2:59am')
、戻ります0259hr
。しかし、文字列を入力すると、コードのこの部分またはこれ12
が自動的に省略され、この部分に進みます。if ('a' and ':') in tstr:
elif ('p' and ':') in tstr:
if tstr[:2] == '12':
tstr = tstr.replace('12', '00').replace(':','').replace('am','hr')
return tstr
したがって、12:15am
またはを入力しても12:15pm
、このコードが文字列内で見つかった場合、12
上記のコードを実行し始めます。print time24hr('12:15pm')
を返しますが、含まれる文字列に対してのみ返す0015pm
必要があります。それ以外の場合は、に変更して返さないでください。0015hr
am
12
00
1244hr
12:44pm
私の質問は、なぜそれらの論理チェックが機能if ('a' and ':') in tstr:
しelif ('p' and ':') in tstr:
ないのですか? このコードは、このクイズの解決策を意図しています - > http://www.pyschools.com/quiz/view_question/s3-q8
================================================== ================================
論理演算で私を助けてくれてありがとう。
また、上記のクイズを完了しました。ここに作業コードがあります。
def time24hr(tstr):
if (len(tstr[:tstr.find(':')]) == 2) and (tstr[0] == '0'):
tstr = tstr.replace(tstr[0], '')
if ('a' in tstr) and (':' in tstr):
if tstr[:2] == '12':
tstr = tstr.replace('12', '00').replace(':', '').replace('am', 'hr')
return tstr
elif len(tstr[:tstr.find(':')]) < 2:
# If length of string's 'tstr' slice (that ends before string ':') is less than 2
tstr = tstr.replace(tstr[:tstr.find(':')], '0' + tstr[:tstr.find(':')]).replace(':', '').replace('am', 'hr')
# Replace one-character string with combination of '0' and this one-character string. Then remove colon, by replacing it with lack of characters and replace 'am' string with 'hr'.
return tstr
else:
tstr = tstr.replace(':', '').replace('am', 'hr')
return tstr
elif ('p' in tstr) and (':' in tstr):
if tstr[:2] == '12':
tstr = tstr.replace(':', '').replace('pm', 'hr')
return tstr
elif len(tstr[:tstr.find(':')]) < 2:
PmDict = {'0':'12','1':'13', '2':'14', '3':'15', '4':'16', '5':'17', '6':'18', '7':'19', '8':'20', '9':'21', '10':'22', '11':'23'}
tstr = tstr.replace(tstr[:tstr.find(':')], PmDict[tstr[:tstr.find(':')]]).replace(':', '').replace('pm', 'hr')
# Replace every "number" (which is string literally) with it's corresponding "number" in 24HR format, found in 'PmDict' dictionary. Then, as in above cases, remove colon ':' by replacing it with lack of character or space and then replace 'pm' with 'hr'
return tstr
else:
return "Time format unknown. Type correct time."
ご覧のとおり、このコードは KISS ルールに従って書いたわけではありません。
ここでテストできます-> http://doc.pyschools.com/console
みんなに乾杯、助けてくれてありがとう:)