Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 9058089
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 16, 20262026-06-16T14:37:58+00:00 2026-06-16T14:37:58+00:00

I want to apply this while loop into the for loop below. I have

  • 0

I want to apply this while loop into the for loop below.
I have tried putting the while loop before the if statements, in each if statement.
When i put it before the if statement( in the for loop ) it asks the user once and then returns the same input for the whole range (1,8).
I want this while loop to apply to each question, the seven items 2 to 8
how do i implement it. can anyone help please, Thanks

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 

End of the while loop

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 ()    

Is the lack of responses a sign that its a stupid question or is it not possible to do?

does this make it any clearer ?

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
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-16T14:37:59+00:00Added an answer on June 16, 2026 at 2:37 pm

    You could try something like this (apologies if this isn’t what you were looking for). Happy to explain anything that doesn’t make sense – overall idea is that it loops through a range of 8 items, asking for a valid price each time and continuing to ask if the entered value is either not a number or not in the specified range. As this may be for an assignment, I tried to keep it as closely aligned to the concepts you already demonstrated that you knew (the only exception here may be 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()
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I want apply margin property for my html newsletter But Gmail ignores this CSS
I want to apply a Storyboard to my Rectangle Fill like this: <Rectangle Name=MyRectangle
I want to apply cycle to the div block with dynamic content. This content
this is another question that I want to know that how to apply any
I want to apply a regex on lines where I have only 2 words.
I have a data.frame df like this: df <- data.frame (x=1:5,y=1:5) I want to
I have researched this topic for a while, but without much success. I did
I have a do loop: <% @currentfruit.each do |apples| %> <%= apples.prices %> <%
I want to apply different lighting on white and black boxes in this checkerboard.So
I have a school project and i want to dedicate this for New Year,

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.