1

我想将此 while 循环应用到下面的 for 循环中。我尝试在每个 if 语句中将 while 循环放在 if 语句之前。当我将它放在 if 语句之前(在 for 循环中)时,它会询问用户一次,然后为整个范围(1,8)返回相同的输入。我希望这个 while 循环适用于每个问题,即 2 到 8 的七个项目我如何实现它。谁能帮忙,谢谢

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 回答