尝试制作列表时出现TypeError:不可按顺序比较的类型:list()<= int()

2024-06-06 08:40:21 发布

您现在位置:Python中文网/ 问答频道 /正文

尝试编写一个代码,允许用户在一个空列表中填写房价,然后通过给一个新的值进行排序。你知道吗

import Utils2

do_again = 'yes'

house_price = []

while do_again == 'yes' :
    price =Utils2.get_int('Please enter house price:')
    house_price.append(price)
    do_again = Utils2.yes_or_no('try another (yes or no)?')
#end while
mid_range_house = []
low_price_house =[]
high_price_house=[]

print('the price of houses is :')
for each in house_price:
    if (mid_range_house <=each ):
        low_price_house.append(each)
    else:
        high_price_house.append(each)

Tags: ornorangedopriceyeshouselow
1条回答
网友
1楼 · 发布于 2024-06-06 08:40:21

您正在将列表与整数进行比较:

if (mid_range_house <=each ):

其中mid_range_house是列表,each是整数。你知道吗

不清楚您试图用代码实现什么,但是您需要将数字与其他数字进行比较。你知道吗

如果您想找到中间房价,请对house_price列表进行排序,然后选择中间值:

median = sorted(house_price)[len(house_price) // 2]

相关问题 更多 >