My loop is "supposed" to go through the list from [0] to the final item which is at most [7] and then purchase and count how many times it purchased that item from the list and then multiply the result by a value.
What it is currently doing is purchasing a low number of items, outputting how many times each has been purchased and thats it. It isn't even "spending" all of its "allowance"
and it generally outputs
CP Total: 2000
Type Item: Item
# of Properties: 5
{'MDEF': 4}
{'SPR': 3}
{'AGI': 2}
{'CRIT': 3}
{}
Which if you see the code and add the costs together it only adds up to 100-200 at most not anywhere near 2000
for aProp in S:
cost = CostDic[aProp]
if (cost * 5) < CP:
purchase_ls = []
while CP >= cost:
b = randint(1, 5)
cost = cost * b
if CP < cost:
continue
purchase_ls.append(aProp)
CP = CP - cost
else:
purchase_ls = []
while CP >= cost:
cost = cost
if CP < cost:
continue
purchase_ls.append(aProp)
CP = CP - cost
print(dict(Counter(purchase_ls)))
Is the section in particular that is causing me grief
Here is your problem:
while CP >= cost:
b = randint(1, 5)
cost = cost * b
# More stuff follows...
In this loop you have now modified the value of cost
, which I'm guessing you didn't mean to do. Specifically, in the line cost = cost * b
I believe your intent was that cost
on the right-hand side is representing the cost of an individual unit, and the cost
on the left-hand side is representing the cost of "n" units, where "n" is a random number between 1 and 5.
But what this code is actually doing is different from that intention. The second time you enter that while
loop, the variable cost
is still at its new, higher value, and then will get multiplied again by a random number 1-5. So at this rate cost
will quickly grow too large for CP >= cost
to remain true.
I think what tripped you up here is that you used the same variable cost
to represent different things at different times. I'd strongly suggest you change this into two different variables with more meaningful names, for example unitCost
and groupCost
, to prevent this type of confusion in the code later on.
For the loop in question, i have now modified it to
for aProp in S:
cost = CostDic[aProp]
if (cost * 5) < CP:
purchase_ls = []
while CP >= cost:
b = randint(1, 5)
mcost = cost*b
if CP < mcost:
continue
purchase_ls.append(aProp)
CP = CP - mcost
else:
purchase_ls = []
while CP >= cost:
if CP < cost:
continue
purchase_ls.append(aProp)
CP = CP - cost
print(dict(Counter(purchase_ls)))
Which does the looping part fine but it isn't doing 2 things correctly like i had hoped
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.