1

私はこのコードを持っています:

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必要があります。それ以外の場合は、に変更して返さないでください。0015hram12001244hr12: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

みんなに乾杯、助けてくれてありがとう:)

4

4 に答える 4

6
if ('a' and ':') in tstr:

と同じです

if ':' in tstr:

これにより、どのような問題があるかについての洞察が得られることを願っています。

おそらく

if 'a' in tstr and ':' in tstr:
于 2012-08-17T14:16:41.827 に答える
0

を使用して、複数の要素の存在についてこのテストを一般化できますall

if all(ch in tstr for ch in 'a:'):
    etc...
于 2012-08-17T14:30:20.390 に答える
0

あなたの最初のチェック:

('a' and ':') in tstr

':' が tstr にあるかどうかを実際に確認しているだけです。正確な理由('a' and ':')はわかりませんが、インタラクティブな Python でステートメントを実行すると、':' が返されることがわかります。

2 番目のチェックには同じエラーがあります。

使用を検討してください

if ':' in tstr and 'a' in tstr:

elif 'p' in tstr and ':' in tstr:

残念ながら、Python は英語のようにしかできません :)

于 2012-08-17T14:17:05.053 に答える
0

あなたの主な問題は、部分文字列が含まれているかどうかをチェックするコードが正しくないことですin。不適切に使用しています。あなたが書くとき

if ('a' and ':') in tstr:

('a' and ':')最初に getを評価し:、次に をチェックしますif ':' in tstrifこれが、最初のブロックが true に評価され続ける理由です。これandは、 がブール演算であり、x and yと同等であるために発生しx if bool(x) else yます。この式は、inチェックに到達する前に評価されます。

そのコードを次のように置き換えます

if ('a' in tstr) and (':' in tstr)

(そして、他のチェックについても同様に行います)

于 2012-08-17T14:20:10.873 に答える