Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/352.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
如何在if和else语句(在python中)中定义for循环的结果?_Python_Python 3.x_For Loop - Fatal编程技术网

如何在if和else语句(在python中)中定义for循环的结果?

如何在if和else语句(在python中)中定义for循环的结果?,python,python-3.x,for-loop,Python,Python 3.x,For Loop,我的代码可能看起来很混乱,所以我将一步一步地进行。以下是我所做的工作: 我有6张单子。我的代码询问用户购物车中有多少物品。它获取该数字并打印从列表中选择的随机项目数,例如,如果用户为购物车中的项目输入4,则可能会生成购物车项目的随机列表:[‘菠萝’、‘iPhone手机壳’、‘工具箱’、‘橄榄’] 我的问题: 如何定义随机项目列表,以便在打印“菠萝”时在账单中添加1 items_1 =[ "soap","ketchup","pineapple","crisp","twix"] items_2 =

我的代码可能看起来很混乱,所以我将一步一步地进行。以下是我所做的工作:

我有6张单子。我的代码询问用户购物车中有多少物品。它获取该数字并打印从列表中选择的随机项目数,例如,如果用户为购物车中的项目输入4,则可能会生成购物车项目的随机列表:[‘菠萝’、‘iPhone手机壳’、‘工具箱’、‘橄榄’]

我的问题: 如何定义随机项目列表,以便在打印“菠萝”时在账单中添加1

items_1 =[ "soap","ketchup","pineapple","crisp","twix"]
items_2  = ["olives","mouse pad","shampoo","coke","ruler","pen"]
items_3 =  ["honey","mirror","chocolate bar","fanta"]
items_4 = ["candle","doughnuts","pencil","dr pepper","broccoli","cabbage"]
items_5 = ["book","butter","jam","umbrella","toolbox","knife"]
items_6 = [ "tissue","iphone case","jewels","sprite"]
list_of_lists = [items_1, items_2, items_3, items_4, items_5, items_6]
item_cart=int(input("how many items in the cart"))
scan10=(random.choice(random.choice(list_of_lists)))

for scan in range(item_cart):
     scan1=print(random.choice(random.choice(list_of_lists)))

#BillCreating
print("here are your items")
bill=0

if "soap" in scan1: 
   bill+1
if "ketchup" in scan1: 
   bill+1
if "pineapple" in scan1: 
   bill+1
if "crisp" in scan1: 
   bill+1
if "twix" in scan1: 
   bill+1



print("total:",(bill))
使用+=

IIUC,您希望从6个项目列表中的任意一个列表中提取随机ItMyCART项,请首先通过将ListSoopOFLIST展开,然后汇总所有可计费项来加强代码。p> 在这种方法中,您不必担心按顺序增加账单数量:

import numpy as np

item_list = [item for i_list in list_of_lists for item in i_list]
item_cart = int(input("how many items in the cart"))
scan1 = np.random.choice(item_list, size=item_cart)
billable_items = ["soap", "ketchup", "pineapple", "crisp", "twix"]

bill = sum([1 for b_item in billable_items if b_item in scan1])

print(f"here are your items: {scan1}")
print(f"total: {bill}")
如果您可以使用熊猫,这将变得更加紧凑:

import pandas as pd

bill = pd.Series(billable_items).isin(scan1).sum()
bill+1将bill和1相加,然后返回结果

我想你希望账单增加1,在这种情况下,你需要实际保存结果

bill = bill + 1
Python和许多其他语言都有这样做的速记

bill += 1

I&39;我对你想做什么有点不清楚,但是如果我读对了,你应该用bill=+1替换所有的bill+1。这将增加您的帐单计数,如果您指定的项目之一是从随机调用返回的。由于混合使用str和int类型,print语句可能会崩溃。将其替换为printtotal:{}.formatbill.抱歉这么含糊,您的评论增加了我的账单数量,这对我来说是第一次:但是,手推车中有多少物品12菠萝香皂dr pepper crisp洗发水书twix番茄酱iphone手机壳镜子纸巾可乐这里是您的物品总数:1事件尽管if声明中有更多物品,但它仍然仅将账单格式精确地设置为1,我希望每次从我的if语句中随机调用一个单词,将1添加到账单中,因为if子句中的所有项目都在第一个列表中,我不觉得这个计数太令人惊讶。您可以同时选择随机列表和随机项目。你觉得奇怪吗?事实上我同意,现在看看。我如何操作我的代码,如果每次随机调用都来自items_1,那么账单上就会增加1。因此,我可以将其复制到我所有的ListCode中,仅在堆栈溢出时不鼓励使用答案。请考虑编辑来解释你做了什么以及为什么它会起作用。因为我在与用户的初始对话中已经这样做了,并且因为我是通过电话回复的,所以我发现在这种情况下这是多余的。在回答中建立完整的上下文非常重要,这样当人们通过谷歌访问此页面时,他们可以轻松获得完全理解答案所需的所有信息。记住这是QA网站,不是论坛。我们并没有试图帮助这个特定的用户,我们只是试图帮助每个人解决这个问题。事实上,我们是。谢谢你在这件事上帮助我。
bill += 1
bill=0

for i in range(item_cart):
    scan1=random.choice(random.choice(list_of_lists))
    if scan1 in ["twix", "pineapple",....]:
        bill+=1

print("here are your items")
print("total: {}".format(bill))