简体   繁体   中英

How do i make a program that makes automated candy packs and a list of the items it added in Python?

So hello, i am trying to make a program that makes candy packs automatically. How i want it to work is basically use a while loop. You set a wished price as a Float number and the program picks out random items from a list until the select price is full or a little overboard. After that i want it to add the names of the items it picked in a list. The aproach i tried was making a list of every item and element "0" contains the price and element "1" contains the name. So basically when im adding the price to the "Price" variable im picking element "0" and when i append it to the "selected items" list i append the element "1"

here is the code i came up with so far

print("hello World")
from random import randint

cheapCandy = [0.04, "cheap Candy"]
candyPack = [0.16, "candy Pack"]
mentosPack = [0.49, "mentos Gum"]
smallChocolate = [0.4, "small Chocolate"]

items = [cheapCandy, candyPack, mentosPack, smallChocolate]
selected = []


e = int(input("price range do you want?"))
price = 0
float(price)
while price < e:
    i = randint(0,len(items)-1)
    price += items[i[0]]
    selected.append(items[i[1]])
print("your total worth of items is", price, "4")

print("and you can sell that for around", price + 1, "$, or", price+1.5, "$")

the error was here:

price += items[i[0]]
selected.append(items[i[1]])

so you can change those lines to this:

price += items[i][0]
selected.append(items[i][1])

i also recomend you to use f format strings. Like this:

print(f"your total worth of items is {price} 4")
print(f"and you can sell that for around {price + 1}$, or {price + 1.5}$")

here is some link to the f-format strings: https://realpython.com/python-f-strings/

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