打印特定字符串后中断循环 python

Interrupt loop python after a certain string have been printed

我有一个这样的自行车清单:

fmnewlist = [
    ['Bike No.', 'Purchase Date', 'Batt %', 'Last Maintenance', 'KM since Last', 'Reason/s'], 
    ['T101', '10/4/2016', '55', '10/1/2017', '25.08', 'No service'], 
    ['T102', '1/7/2016', '10', '15/5/2017', '30.94', 'No service'], 
    ['T103', '15/11/2016', '94', '13/6/2017', '83.16', 'KM'], 
    ['T104', '25/4/2017', '58', '10/1/2017', '25.08', 'No service'], 
    ['T105', '24/5/2017', '5', '20/6/2017', '93.8', 'Batt & KM']
]

并且我想检查每个内部列表中的某些元素,然后像这样打印一个字符串而不循环。这是我的代码,为什么它不起作用?

lens=len(fmnewlist)
biketocheck=(input("Bike No.:"))
countertwo=1

while countertwo < (lens+1):
    a=fmnewlist[countertwo]
    if biketocheck in a[0] and a[5]!="No service":
        print("Bike serviced.")
    elif biketocheck in a[0] and a[5]=="No service":
        print("Bicycle not due for servicing")
    elif biketocheck not in fmnewlist:
        print("No such bicycle")
    else:
        print(" ")
    countertwo+=1
    if countertwo==lens:
        break

输入 T123 时的预期输出:

Bike No.:T123
No such bicycle

实际输出:

Bike No.:T123
No such bicycle
No such bicycle
No such bicycle
No such bicycle
No such bicycle

输入为 T102 时的预期输出:

Bicycle not due for servicing

实际输出:

Bike No.:T102 No such bicycle Bicycle not due for servicing No such bicycle No such bicycle No such bicycle

输入为 T103 时的预期输出:

Bike serviced 实际输出:

Bike No.:T103 No such bicycle No such bicycle Bike serviced. No such bicycle No such bicycle

为什么不直接创建列表的查找地图并进行快速 if .. in 比较,例如:

fmnewlist = [
    ['Bike No.', 'Purchase Date', 'Batt %', 'Last Maintenance','KM since Last', 'Reason/s'], 
    ['T101', '10/4/2016', '55', '10/1/2017', '25.08', 'No service'], 
    ['T102', '1/7/2016', '10', '15/5/2017', '30.94', 'No service'], 
    ['T103', '15/11/2016', '94', '13/6/2017', '83.16', 'KM'], 
    ['T104', '25/4/2017', '58', '10/1/2017', '25.08', 'No service'], 
    ['T105', '24/5/2017', '5', '20/6/2017', '93.8', 'Batt & KM']
]

bike_iter = iter(fmnewlist)  # create an iterator for easier handling
next(bike_iter)  # skip the first item (header)
bike_nos = {bike[0]: bike[5] for bike in bike_iter}  # create bike_no: reason map

biketocheck = input("Bike No.: ")  # get the input

if biketocheck in bike_nos:  # check if the bike exists
    if bike_nos[biketocheck] == "No service":  # if it exists, check for status...
        print("Bicycle not due for servicing")
    else:
        print("Bike serviced.")
else:  # bike doesn't exist...
    print("No such bicycle")

如果你测试它:

Bike No.: T103
Bike serviced.

Bike No.: T102
Bicycle not due for servicing

Bike No.: T123
No such bicycle

使用for代替while,删除else部分。然后你可以使用 else 声明 for:

for bicycle in fmnewlist[1:]:  # Don't use the first line
    # previous statements except else, it should have a `break` too.
    if biketocheck in a[0]:
        if a[5]!="No service":
            print("Bike serviced.")
        # compare is either != or == so there's no need to write the whole elif 
        else:
            print("Bicycle not due for servicing")
        break
else:
    print("No such bicycle")

else 只有在 for 没有中断的情况下才会被使用。所以这意味着没有找到自行车。你正在做的是检查每一行是否有所需的自行车,当然它不存在于那个元素中。

P.S:您也可以将 elsewhile 一起使用。但是你必须确定条件 returns False after a while 或 else 子句将不起作用。使用 for 更容易做到这一点。

以下是我对该程序应该做什么的最佳猜测:

  1. 在自行车列表中查找第一个元素与用户指定的 ID 匹配的行。
  2. 如果没有找到自行车,打印"No such bicycle."
  3. 如果找到自行车,根据第 6 个元素的值打印出 "Bicycle not due for servicing" 或 "Bike serviced"。

鉴于这些假设,我相信这段代码应该可以满足您的要求:

fmnewlist = [
    ['Bike No.', 'Purchase Date', 'Batt %', 'Last Maintenance', 'KM since Last', 'Reason/s'], 
    ['T101', '10/4/2016', '55', '10/1/2017', '25.08', 'No service'], 
    ['T102', '1/7/2016', '10', '15/5/2017', '30.94', 'No service'], 
    ['T103', '15/11/2016', '94', '13/6/2017', '83.16', 'KM'], 
    ['T104', '25/4/2017', '58', '10/1/2017', '25.08', 'No service'], 
    ['T105', '24/5/2017', '5', '20/6/2017', '93.8', 'Batt & KM']
]

biketocheck=(input("Bike No.:"))

found = False
for bike in fmnewlist[1:]:
    if bike[0] == biketocheck:
        found = True

        if bike[5] == "No service":
            print("Bicycle not due for servicing")
        else:
            print("Bike serviced.")
        break

if not found:
    print("No such bicycle")

编辑

改用 dict,这使逻辑更简单:

bikes = { bike[0]:bike[1:] for bike in fmnewlist[1:] }

biketocheck=(input("Bike No.:"))

if biketocheck in bikes:
    if bikes[biketocheck][4] == "No service":
        print("Bicycle not due for servicing")
    else:
        print("Bike serviced.")
else:
    print("No such bicycle")