繁体   English   中英

即使两个选择都不是,输出也不会打印结果,但第一个选项是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的西尔富酮价格都没有结果,但是我没有输入较高价格,也没有输入搜索品牌。 输出为“您未输入任何搜索参数”。

我的代码段如下:

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()

买方模式

============================ [1]显示所有celfones [2]根据价格和品牌搜索西尔芬

[3]返回上一级菜单

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

[]

选择>>> 1列表中有2 celfones。 1. Lumia,诺基亚,Php3.0功能:2. Xperia,索尼,Php4.0功能:

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

深入了解以下代码行:

    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":无。 因此,只有添加“无”品牌电话,您才能找到任何匹配项。 此外,它第一次通过temp[2] < lower_price的循环运行时,它将退出循环,而忽略以后的任何匹配对象。

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

感谢您的输入。 我已经找到解决此问题的方法。 我只是对该功能进行了重大修改。

主要区别在于:

  • 我立即在三个选择之后放置了for循环。
  • 我删除了几个for循环。
  • 我也很难为if和elif语句确定和应用真值表。 (我只是编程的新手)
  • 另外,我还从所有语句中删除了该中断。 感谢您的输入@Fawful
  • 另外,对于某些语句,我将“无”更改为“无”。 感谢您的输入@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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM