Python认为100小于50?

时间:2017-01-24 16:18:19

标签: python arrays windows file numbers

这是我的代码:

stock_file  = open('Task_3_1.txt', "rt") #open file
stock_lines = stock_file.readlines() #convert items to list
total_buys = []
total_cost = []
done = 0
for o in range(0,(len(stock_lines)-1)): 
    edit = list(stock_lines[o])
    for m in range(1,2): #convert file to array
        edit.remove(edit[len(edit)-1])
    stock_lines[o] = ''.join(edit)
to_do = input("1. Find items that need to be restocked\n")
if to_do == "1":    
    x = 1
    while x < len(stock_lines):
        if stock_lines[x] < stock_lines[x+1]:        
            print(stock_lines[x-1], "needs restocking (" + stock_lines[x] +  " stock, restock level is", stock_lines[x+1] +")")
        x+=4 #increases x to check the next item

我的问题是,在运行时,它意味着打印出需要补货的东西(哪些库存水平低于补货水平)。这是我使用的股票档案:

Egg
100
50
75
Crocodile clip
30
12
30
Chocolate
206
300
390

第一行是物品名称,然后是库存水平,然后是库存水平,然后是目标库存水平(重新进货时物品的库存量)。

当我运行时:

1. Find items that need to be restocked
1
Egg needs restocking (100 stock, restock level is 50)
Chocolate needs restocking (206 stock, restock level is 300)

正如你所看到的,它告诉我100小于50.任何帮助都会很棒。

我已经尝试将100更改为101以查看双零是否会影响它。

3 个答案:

答案 0 :(得分:2)

>>> '100' < '50'
True
>>> 100 < 50
False

答案 1 :(得分:1)

您正在比较字符串

"100" < "50"

因为1来自5。

修复如下:

if int(stock_lines[x]) < int(stock_lines[x+1]):     

答案 2 :(得分:0)

更改为int(stock_lines[x]) < int(stock_lines[x+1])

相关问题