3

私は Learn Python the Hard Way の演習 35 を行っています。以下は元のコードで、0 と 1 だけを含まない数字を受け入れるように変更するよう求められています。

def gold_room():
    print "This room is full of gold. How much do you take?"

    next = raw_input("> ")

    if "0" in next or "1" in next:
        how_much = int(next)

    else:
        dead("Man, learn to type a number.")

    if how_much < 50:
        print "Nice, you're not greedy, you win!"
        exit(0)

    else:
        dead("You greedy bastard!")

これは私のソリューションであり、正常に動作し、浮動小数点値を認識します。

def gold_room():
    print "This room is full of gold. What percent of it do you take?"

    next = raw_input("> ")

    try:
        how_much = float(next)
    except ValueError:
        print "Man, learn to type a number."
        gold_room()

    if how_much <= 50:
        print "Nice, you're not greedy, you win!"
        exit(0)

    else:
        dead("You greedy bastard!")

同様の質問を検索すると、次のコードに示す別のソリューションを作成するのに役立ついくつかの回答が見つかりました。問題は、 isdigit() を使用すると、ユーザーが float 値を入力できないことです。したがって、ユーザーが 50.5% を受け取りたいと言った場合、数字の入力方法を学ぶように指示されます。それ以外の場合は整数に対して機能します。どうすればこれを回避できますか?

def gold_room():
    print "This room is full of gold. What percent of it do you take?"

    next = raw_input("> ")

while True:
    if next.isdigit():
        how_much = float(next)

        if how_much <= 50:
            print "Nice, you're not greedy, you win!"
            exit(0)

        else:
            dead("You greedy bastard!")

    else: 
        print "Man, learn to type a number."
        gold_room()
4

6 に答える 6

1

私があなたのアプローチで抱えている問題は、より Pythonic な「許可よりも許しを求めやすい」パスではなく、「Leap Before You Leap を見る」パスをたどっていることです。この方法で入力を検証しようとするよりも、元のソリューションの方が優れていると思います。

これが私がそれを書く方法です。

GREEDY_LIMIT = 50

def gold_room():
    print("This room is full of gold. What percent of it do you take?")

    try:
        how_much = float(raw_input("> "))
    except ValueError:
        print("Man, learn to type a number.")
        gold_room()
        return

    if how_much <= GREEDY_LIMIT:
        print "Nice, you're not greedy, you win!"
        exit(0)

    else:
        dead("You greedy bastard!")
于 2014-04-25T22:24:47.130 に答える
0

try/except を使用したくない場合は、以下が私の答えです。

def gold_room():
    print "This room is full of gold. How much do you take?"

    choice = input("> ")

    if choice.isdigit():
        how_much = int(choice)
    elif "." in choice:
        choice_dot = choice
        choice_dot_remove = choice_dot.replace(".","")
        if choice_dot_remove.isdigit():
            how_much = float(choice)

    else:
        dead("Man, learn to type a number.")

    if how_much < 50:
        print "Nice, you're not greedy, you win!"
        exit(0)

    else:
        dead("You greedy bastard!")
于 2019-05-16T14:41:48.627 に答える
0

以下のpythonベースの正規表現を使用して、float文字列をチェックします

import re
a=re.match('((\d+[\.]\d*$)|(\.)\d+$)' ,  '2.3') 
a=re.match('((\d+[\.]\d*$)|(\.)\d+$)' ,  '2.')
a=re.match('((\d+[\.]\d*$)|(\.)\d+$)' ,  '.3')
a=re.match('((\d+[\.]\d*$)|(\.)\d+$)' ,  '2.3sd')
a=re.match('((\d+[\.]\d*$)|(\.)\d+$)' ,  '2.3')

output: !None, !None, !None, None , !None この出力を使用して変換を行います。

于 2017-02-24T11:03:42.157 に答える