2016-03-11 33 views
0

所以我试图设计两个函数,一个创建一个列表,一个检查列表中的某些参数。功能check()功能是查看函数的随机生成列表中的任何元素是否> 100,然后通知用户需要记录哪些元素。说实话,我真的不知道从哪里开始check()函数,并希望有人可能有一些建议?If/Else Statement and Lists Within Functions - Python 3.x

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 

我知道check()功能的路要走,但就像我说的,不完全知道从哪里去用它。

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

感谢您的高级帮助!

+1

您是否需要检查'priceList'的所有元素是否都小于100?或者找到哪些元素比100大? –

+1

'checkthese = [p for priceList如果p> 100]' – mpez0

+0

价格表中没有任何初始值。您可以将price()的第一行更改为'priceList = []',因为无论如何,所有项目都将被随机值替换。 –

回答

1

这似乎是最好的方法。

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 

并用于检查列表。

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

最简单的解决方案是循环传递给检查函数的值。

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

如果您愿意,您可以使用price()生成价格表并将其传递给check()。

check(price())