简体   繁体   中英

How can you define a for loop's outcome in an if and else statement (in python)?

My code may look confusing, so I will go step by step. Here is what I have done (that works):

I have a 6 lists. My code asks the user how many items are in the user's cart. It takes that number and prints that number of random items selected from the lists (eg If a user enters 4 for items in cart , this random list of cart items might be generated: ['pineapple', 'iPhone case', 'toolbox', 'olives'] ).

My question: How can I define the random item lists so if 'pineapple' is printed it would add 1 to the bill?

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

Use += .

if "pineapple" in scan1: 
    bill += 1

IIUC, and you want the random item_cart items to be drawn from any of the 6 item lists, consider tightening up your code by flattening list_of_lists first, and then summing over all billable items.

In this approach, you don't need to worry about incrementing the bill count sequentially:

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}")

If you can use Pandas, this gets even more compact:

import pandas as pd

bill = pd.Series(billable_items).isin(scan1).sum()

bill+1 adds bill and 1 together - and then returns the result.

I expect you want bill to increase by one, in which case you would need to actually save the result

bill = bill + 1

Python (and many other languages) have a shorthand for doing this.

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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