0
howManyNames = (float(input("Enter how many student names do you want to enter? ")))
studentNames = []
ages = []
averageAge = 0
counter = 0

while (counter < int(howManyNames)):
    studentNames.append(input("Enter student names. "))
    ages.append(float(input("Enter that persons age. ")))
    counter += 1
    averageAge = sum(ages) / float(howManyNames)

while (ages[:] < int(averageAge)): ## crashes because its comparing a list < int  
    print ("Your age is below the average age.", ages)
    if ages > averageAge:
        print ("Your age is above the average age.", ages)
        break

我收到错误消息 TypeError: unorderable types: list() < int() 我什至尝试了切片以查看是否有效.....

4

2 回答 2

1

我想这就是你的意思,不是吗?

评估ages变量中的年龄是否小于或大于平均值。

for age in ages:
   if age < int(averageAge):
      print ("Your age is below the average age.", age)
   else:
      print ("Your age is above the average age.", age)

循环while将继续运行,直到条件不再为真。

while condition is true:
       #do something

你说的没有任何意义。你说while [1, 2, 3] < 4:这就像说while speed < car你没有比较任何有效的东西。由于它是一个列表,因此您需要对每个对象进行迭代(使用 for 循环),然后将其与您的平均值进行比较。这是一个有效的比较。我希望这能解释问题

于 2013-11-13T19:45:00.817 回答
0

您试图将整个年龄列表与平均年龄进行比较,而不是像 jramirez 证明的那样比较列表中的每个单独元素/年龄。

于 2013-11-13T19:48:51.790 回答