1

このwhileループを以下のforループに適用したいと思います。各ifステートメントで、ifステートメントの前にwhileループを配置してみました。ifステートメントの前(forループ内)に置くと、ユーザーに1回尋ねてから、範囲全体(1,8)に対して同じ入力を返します。このwhileループを各質問に適用したいのですが、2から8の7つの項目をどのように実装すればよいですか。誰か助けてくれませんか、ありがとう

def valid_entry ():
    price = 110
    invalid_input = 1
    while price< 0 or price> 100:
        if invalid_input >=2:
            print "This is an invalid entry"
            print "Please enter a number between 0 and 100"
        try:
            price= int(raw_input("Please enter your price  : "))
        except ValueError:
            price = -1   
        invalid_input +=1 

whileループの終わり

def prices ():   
    x = range (1,8)
    item=2
    price=0
    for item in x:
        item +=1
        print "\n\t\titem",item
        price = int(raw_input("Enter your price : "))
        if price <10:
            price=1
            print "This is ok"


        if price >9 and price <45:
            price +=5
            print "This is great"


        if price >44 and price <70:
            price +=15
            print "This is really great"

        if price >69:
            price +=40
            print "This is more than i expected"

    print "\nYou now have spent a total of ",price

prices ()    

回答がないということは、それがばかげた質問であることを示しているのでしょうか、それともできないのでしょうか。

これはそれをより明確にしますか?

def prices ():   
    x = range (1,8)
    item=2
    price=0
    for item in x:
        item +=1
        print "\n\t\titem",item
        valid_entry ()#should it go here
        price = int(raw_input("Enter your price : "))
        valid_entry ()#should it go here
        if price <10:
           valid_entry ()#should it go here and so on for the following 3 if conditions
            price=1
            print "This is ok"


        if price >9 and price <45:
            price +=5
            print "This is great"


        if price >44 and price <70:
            price +=15
            print "This is really great"

        if price >69:
            price +=40
            print "This is more than i expected"

    print "\nYou now have spent a total of ",price
4

1 に答える 1

1

あなたはこのようなことを試すことができます(これがあなたが探していたものでない場合は謝罪します)。意味をなさないことは何でも説明してください-全体的な考え方は、8つのアイテムの範囲をループし、毎回有効な価格を要求し、入力された値が数値ではないか、指定された範囲内にないかを尋ね続けることです。これは課題の場合もあるので、私はあなたがすでに知っていることを示した概念と同じように密接に一致させようとしました(ここでの唯一の例外はcontinue):

def valid_entry():
    # Here we define a number of attempts (which is what I think
    # you were doing with invalid_input). If the person enters 10
    # invalid attempts, the return value is None. We then check to
    # see if the value we get back from our function is None, and if
    # not, proceed as expected.
    num_attempts = 0
    while num_attempts < 10:
        # Here we do the input piece. Note that if we hit a ValueError,
        # the 'continue' statement skips the rest of the code and goes
        # back to the beginning of the while loop, which will prompt
        # again for the price.
        try:
            price = int(raw_input("Enter your price : "))
        except ValueError:
            print 'Please enter a number.'
            num_attempts += 1
            continue

        # Now check the price, and if it isn't in our range, increment
        # our attempts and go back to the beginning of the loop.
        if price < 0 or price > 100:
            print "This is an invalid entry"
            print "Please enter a number between 0 and 100"
            num_attempts += 1
        else:
            # If we get here, we know that the price is both a number
            # and within our target range. Therefore we can safely return
            # this number.
            return price

    # If we get here, we have exhausted our number of attempts and we will
    # therefore return 'None' (and alert the user this is happening).
    print 'Too many invalid attempts. Moving to the next item.'
    return None


def prices():
    # Here we go from 1-8 (since the upper bound is not included when
    # using range).
    x = range(1,9)

    # Here we use a variable (total) to store our running total, which will
    # then be presented at the end.
    total = 0

    # Now we iterate through our range.
    for item in x:
        print "\n\t\titem",item

        # Here is the important part - we call our valid_entry function and
        # store the result in the 'price' variable. If the price is not
        # None (which as we saw is the return value if the number of attempts
        # is > 10), we proceed as usual.
        price = valid_entry()
        if price is not None:
            if price <10:
                # Note this should probably be += 1
                total += 1
                print "This is ok"

            elif price >= 10 and price < 45:
                total += 5
                print "This is great"

            elif price >= 45 and price < 70:
                total += 15
                print "This is really great"

            # We know that price >= 70 here
            else:
                total += 40
                print "This is more than i expected"

    # Now print out the total amount spent
    print "\nYou now have spent a total of ", total

prices()
于 2013-01-01T20:45:38.760 に答える