簡體   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