繁体   English   中英

我如何制作一个程序来制作自动糖果包和它在 Python 中添加的项目列表?

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

所以你好,我正在尝试制作一个自动制作糖果包的程序。 我希望它如何工作基本上是使用while循环。 您将希望的价格设置为浮点数,程序会从列表中随机挑选商品,直到 select 价格已满或有点过高。 之后我希望它添加它在列表中选择的项目的名称。 我尝试的方法是列出每个项目,元素“0”包含价格,元素“1”包含名称。 所以基本上,当我将价格添加到“价格”变量时,我选择元素“0”,当我 append 将它添加到“选定项目”列表时,我 append 元素“1”

这是我到目前为止想出的代码

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, "$")

错误在这里:

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

所以你可以将这些行更改为:

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

我还建议您使用f format字符串。 像这样:

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

这是 f 格式字符串的一些链接: https://realpython.com/python-f-strings/

暂无
暂无

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

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