0

So I'm trying to design two functions, one to create a list and one to check certain parameters within that list. The check() function is to see if any of the elements of the price() function's randomly generated list are >100 then inform the user which of those elements need to be recorded. To be honest, I'm not really sure where to begin with the check() function and was hoping someone might have some advice?

def price():
    priceList = [1,2,3,4,5,6,7,8,9,10]
    print ("Price of Item Sold:")
    for i in range (10):
        priceList[i] = random.uniform(1.0,1000.0)
        print("${:7.2f}".format(priceList[i]))
    print("\n")
    return priceList

I know the check() function is way off, but like I said, not exactly sure where to go with it.

def check(priceList):
    if priceList > 100
        print ("This item needs to be recorded")

Thank you in advanced for any help!

3
  • 1
    Do you need to check if all elements of priceList are less than 100? Or find which elements are great than 100? Commented Mar 11, 2016 at 17:24
  • 1
    checkthese = [p for p in priceList if p > 100] Commented Mar 11, 2016 at 17:44
  • There's no need to have any initial values in pricelist. You can change the first line of price() to priceList = [], since all the items will be replaced with random values anyway. Commented Mar 11, 2016 at 17:50

2 Answers 2

1

This seems to be the best way to go.

def price():
    priceList = [1,2,3,4,5,6,7,8,9,10]
    print ("Price of Item Sold:")
    for i in range (10):
        priceList[i] = random.uniform(1.0,1000.0)
        print("${:7.2f}".format(priceList[i]))
    print("\n")
    return priceList

And for checking the list.

def check(): #Will go through the list and print out any values greater than 100
    plist = price()
    for p in plist:
        if p > 100:
            print("{} needs to be recorded".format(p)) 
Sign up to request clarification or add additional context in comments.

Comments

1

The simplest solution is to loop over the values passed to the check function.

def check(priceList, maxprice=100):
    for price in priceList:
        if price > maxprice:
            print("{} is more than {}".format(price, maxprice)
            print ("This item needs to be recorded")

You can generate the pricelist with price() and pass it to check() at once, if you like.

check(price())

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.