即使两个选择是 none 但第一个选项是 python 列表中的正确选择,输出也不会打印结果

Output does not print result even though the two choices are none but the first option is the right choice in python list

晚上好。

我想问一下为什么我下面的代码没有结果,即使 3 是 celfone 的价格,我输入了 none 作为上限价格, none 作为上限价格搜索品牌。输出是 "You did not enter any search parameters."

我的代码片段如下:

def search_celfone():
templist = []
temp = []
count = 0

lower_price = input("Enter lower bound of price: ")
upper_price = input("Enter upper bound of price: ")
search_brand = str(input("Enter brand name: ")).upper()

#IF CHOICES 1 AND 2 ARE NONE
if lower_price == "none" and upper_price == "none": 
    for cp in celfones:
        temp = celfones[count]
        count += 1

    #IF CHOICES 1 AND 2 ARE NONE AND CHOICE 3 IS IN THE LIST
        if temp[1] == search_brand:
            templist.append(temp)
            break

    #IF CHOICES 1 AND 2 ARE NONE AND CHOICE 3 IS NOT IN THE LIST INCLUDING NONE
        else:
            temp[1] != search_brand
            continue
        break   

#IF CHOICE 1 IS INTEGER AND CHOICES 2 AND 3 ARE NONE    
elif lower_price != "none" and upper_price == "none" and search_brand == "none":
    lower_price = float(lower_price)
    for cp in celfones:
        temp = celfones[count]
        count += 1


        if temp[1] == search_brand and temp[2] >= lower_price:
            templist.append(temp)
            break
        #elif temp[1] == search_brand and temp[2] >= lower_price:
            #templist.append(temp)
            #break
        #elif 

        #elif temp[1] != search_brand or temp[2] >= lower_price:
        elif temp[1] != search_brand and temp[2] >= lower_price:
            continue
        break               


else:
    print ("you did not enter any search parameter") 
count += 1
print (templist)
buyer_menu()

BUYER MODE

============================== [1] 显示所有celfone [2] 根据搜索celfone 价格和品牌

[3]返回上一级菜单

选择 >>> 2 输入价格下限:3 输入价格上限:none 输入品牌名称:none 您没有输入任何搜索参数

[]

CHOICE >>> 1 There are 2 celfones in the list. 1. Lumia, NOKIA, Php3.0 Features: 2. Xperia, SONY, Php4.0 Features:

================================

深入了解这些代码行:

    if temp[1] == search_brand and temp[2] >= lower_price:
        templist.append(temp)
        break
    elif temp[1] != search_brand and temp[2] >= lower_price:
        continue
    break       

search_brand 是,凭借片段 and search_brand == "none": none。因此,在添加 "none" 品牌 phone 之前,您不会找到任何匹配项。此外,它第一次运行循环 where temp[2] < lower_price 时,它会跳出循环,忽略任何后来的匹配对象。

我建议删除该片段中的两个 break 语句,并完全忽略 if 语句中的 search_brand。

感谢您的投入。我已经找到了解决这个问题的方法。我刚刚对该功能进行了重大修改。

主要区别是:

  • 我在三个选择之后立即放置了for循环。
  • 我删除了里面的几个 for 循环。
  • 我也很难理解和应用 if 和 elif 语句的真相 table。 (我只是编程新手)
  • 此外,我还删除了所有语句中的中断。感谢您的输入@Fawful
  • 此外,我将某些语句的 "none" 更改为 "NONE"。感谢您的输入@PM 2Ring

感兴趣的朋友,下面是我修改后的代码:

for cp in celfones:
    temp = celfones[count]
    tempf = temp[3]
    count += 1

    if lower_price == "none" and upper_price == "none": 

        if temp[1] == search_brand:
            templist.append(temp)

    elif lower_price != "none" and upper_price == "none":
        lower_price = float(lower_price)

        if temp[1] == search_brand and temp[2] >= lower_price:
            templist.append(temp)

        elif search_brand == "NONE" and temp[2] >= lower_price:
            templist.append(temp)

    elif lower_price == "none" and upper_price != "none":
        upper_price = float(upper_price)

        if temp[1] == search_brand and temp[2] <= upper_price:
            templist.append(temp)

        elif search_brand == "NONE" and temp[2] <= upper_price:
            templist.append(temp)

    elif lower_price != "none" and upper_price != "none":
        lower_price = float(lower_price)
        upper_price = float(upper_price)

        if search_brand == "NONE" and temp[2] >= lower_price and temp[2] <= upper_price:
            templist.append(temp)

        elif temp[1] == search_brand and temp[2] >= lower_price and temp[2] <= upper_price:
            templist.append(temp)

    else:
        print ("you did not enter any search parameter") 


print (len(templist), "celfone(s) found.")  ### counting of results