-1

Hi im pretty new to Python so I'm guessing im making a pretty obvious mistake here but basically what i'm trying to get my code to do here is to take in 5 products and their prices from the user, the products are working however when the user enters a price that is above 0 the message saying "Please enter a price above 0" is displayed, this is my code below, any help for a newbie is much appreciated thanks.

#Lists of products and prices

products = []
price = [] #declare lists
total = 0 #declare variable to hold total of prices

#function which is used to read in values to each list in turn 
#range has been set to read in 5 values
def inputPrice():
    for counter in range(0,5):
        valid = False
        print (counter+1, "Enter 5 Products:")
        tempItems = input() #declare temp variable to hold input value
        products.append(tempItems) #add this value into our list
            #when reading in price if statement is added for validation
        print (counter+1, "Enter their prices:")
        tempCost = int(input())
        if tempCost<=0: #validate input
               print ("Incorrect price....")
               print ("Please enter a price above 0")
        else: 
            valid = True
            price.append(tempCost)
  • 1
    Possible duplicate of [Python call function again if incorrect input](https://stackoverflow.com/questions/31737745/python-call-function-again-if-incorrect-input) – Corentin Pane Nov 22 '19 at 09:34
  • You might want to use a [debugger](https://stackoverflow.com/questions/25385173/what-is-a-debugger-and-how-can-it-help-me-diagnose-problems) for that or look at [How to debug small programs](https://ericlippert.com/2014/03/05/how-to-debug-small-programs/) – Andreas Nov 22 '19 at 09:37

2 Answers2

0

Update Your Code add while loop to take prices from user

Lists of products and prices

products = []
price = [] #declare lists
total = 0 #declare variable to hold total of prices

#function which is used to read in values to each list in turn 
#range has been set to read in 5 values
def inputPrice():
    for counter in range(0,5):
        valid = False
        print (counter+1, "Enter 5 Products:")
        tempItems = input() #declare temp variable to hold input value
        products.append(tempItems) #add this value into our list
            #when reading in price if statement is added for validation
        while True:
          print (counter+1, "Enter their prices:")
          tempCost = int(input())
          if tempCost<=0: #validate input
               print ("Incorrect price....")
               print ("Please enter a price above 0")
          else:
            break
        else: 
            valid = True
            price.append(tempCost)
            
inputPrice()
Community
  • 1
  • 1
0

Well the main key here is to loop if the condition didn't occur

products = []
price = [] #declare lists
def inputPrice():
    for counter in range(1,6):
        print ("Enter Product {} out of 5".format(counter))
        tempItems = input() #declare temp variable to hold input value
        while tempItems == '': #validate input
            print ("Incorrect Product ...")
            print ("Please enter a valid product")
            tempItems = input()
            #when reading in price if statement is added for validation
        print ("Enter the price of Product {} out of 5".format(counter))
        tempCost = int(input())
        while tempCost <=0: #validate input
            print ("Incorrect price....")
            print ("Please enter a price above 0")
            tempCost = int(input())
        products.append(tempItems)
        price.append(tempCost)
    print('products' , products)
    print('price' , price)

inputPrice()
Ahmed Soliman
  • 1,662
  • 1
  • 11
  • 16