输出不会打印结果,即使这两个选项为“无”,但第一个选项是python列表中的正确选项
晚上好 我想问,为什么下面的代码没有结果,即使3是celfone的价格,我输入的最高价格没有结果,搜索品牌也没有结果。输出为“您没有输入任何搜索参数” 下面是我的代码片段:输出不会打印结果,即使这两个选项为“无”,但第一个选项是python列表中的正确选项,python,list,nested,Python,List,Nested,晚上好 我想问,为什么下面的代码没有结果,即使3是celfone的价格,我输入的最高价格没有结果,搜索品牌也没有结果。输出为“您没有输入任何搜索参数” 下面是我的代码片段: def search_celfone(): templist = [] temp = [] count = 0 lower_price = input("Enter lower bound of price: ") upper_price = input("Enter upper bound of price: ") se
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个Celfone。
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
是,由于代码段和search_brand==“none”:
none。因此,在添加“无”品牌手机之前,您不会找到任何匹配项。此外,当它第一次在temp[2]
的循环中运行时,它将跳出循环,忽略任何稍后匹配的对象
我建议删除该代码段中的
break
语句,并在if
语句中完全忽略搜索品牌 谢谢你的意见。我已经找到了解决这个问题的办法。我刚刚对函数做了一个重大修改
主要区别是:
- 我立即将for循环放在三个选项之后
- 我删除了几个循环内
- 对于if和elif语句,我也很难计算和应用真值表。(我只是个编程新手)
- 另外,我还删除了所有语句中的中断。感谢@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
celfones中cp的:
temp=celfones[计数]
tempf=temp[3]
计数+=1
如果较低的价格=“无”,而较高的价格=“无”:
如果温度[1]==搜索品牌:
templist.append(临时)
elif更低的价格!=“无”和上限价格==“无”:
较低价格=浮动(较低价格)
如果temp[1]==搜索品牌,temp[2]>=更低的价格:
templist.append(临时)
elif搜索品牌==“无”,临时[2]>=较低价格:
templist.append(临时)
elif下限价格==“无”和上限价格!=“无”:
上限价格=浮动(上限价格)
如果temp[1]==search\u brand和temp[2]也注意到输入的search\u brand
字符串被转换为大写,因此如果输入“none”,它实际上将是“none”。@pm2为真,很好。在这种情况下,jeb,您还应该将和search_brand==“none”:
更改为和search_brand==“none”:
。否则,您将无法获得与if语句匹配的内容。尝试从代码片段中删除“break”,但它不会打印价格为3的诺基亚品牌。我正在尝试测试一个输入,比如3,这是诺基亚的价格,然后在较高的价格中键入none,在搜索品牌中键入none。比如,3和none,none,但是它仍然会有诺基亚的输出,因为3是诺基亚的价格。谢谢@jeb您是否已将和搜索品牌==“无”:
更改为和搜索品牌==“无”:
?如果没有,那么由于行search\u brand=str(输入(“输入品牌名称”).upper()
,尤其是.upper()
,您将永远无法输入代码的这一部分。@Fawful yes。#如果选项1为整数,选项2和3为无,则价格较低!=“无”和上限价格==“无”和搜索上限价格==“无”:下限价格=celfones中cp的浮动(下限价格):temp=celfones[count]count+=1如果temp[1]==搜索上限价格和temp[2]>=下限价格:templist.追加(temp)elif temp[1]!=搜索品牌和温度[2]>=更低价格:继续