0

Though it needs some organizational cleanup(am new to python, still improving, feel free to give all suggestions/critique), I'm not sure why the conditional block is returning 'pack1' to bestValueNamed when it should only do that when it is > either one of the two(in this case it is not: see test values).

Section in question is between double asterisks(not sure if it will make it bold). Thanks in advance. :)

The test values I used were:

totalCardsInSet: 8 
listOfCardValues: [1, 2, 3, 4, 5, 6, 7, 8]
pack1Cards: [1, 2]
pack2Cards: [3, 4]
pack3Cards: [5, 6]

And here's the code:

setOfCards = []
listOfCardValues = []

totalCardsInSet = int(input("How many total cards in the set?\n> "))

print("List each value, in order of card number, in cents, 1 per line.")

index = 0
for card in range(totalCardsInSet):
    value = int(input("> "))
    listOfCardValues.append(value)
    setOfCards.append(index)
    index += 1

print(setOfCards)
print(listOfCardValues)

pack1Cards = []
pack2Cards = []
pack3Cards = []

cardsInPack = int(input("How many cards in a pack?\n> "))
for packNo in range(0, 3):
    print(f"Input the card values in pack {packNo + 1}")
    if packNo == 0:
        for cardVal in range(0, cardsInPack):
            pack1Input = int(input("> "))
            pack1Cards.append(pack1Input)
    elif packNo == 1:
        for cardVal in range(0, cardsInPack):
            pack2Input = int(input("> "))
            pack2Cards.append(pack2Input)
    else:
        for cardVal in range(0, cardsInPack):
            pack3Input = int(input("> "))
            pack3Cards.append(pack3Input)

print(pack1Cards)
print(pack2Cards)
print(pack3Cards)

packValues = {
    'sum1': sum(pack1Cards),
    'sum2': sum(pack2Cards),
    'sum3': sum(pack3Cards),
}

**
bestValueNamed = []
bestValue = []

if packValues['sum1'] > packValues['sum2'] or packValues['sum3']:
    bestValueNamed.append('pack 1')
    bestValue.append(packValues['sum1'])
    if packValues['sum2'] > packValues['sum3']:
        bestValueNamed.append('pack 2')
        bestValue.append(packValues['sum2'])
    else:
        bestValueNamed.append('pack 3')
        bestValue.append(packValues['sum3'])
else:
    bestValueNamed.extend(['pack 2', 'pack3'])
    bestValue.extend([packValues['sum2'], packValues['sum3']])
**

print(bestValueNamed)
print(packValues['sum1'])
print(packValues['sum2'])
print(packValues['sum3'])


print(f"The two packs with most value are {bestValueNamed[0]} and {bestValueNamed[1]} worth {sum(bestValue)} cents.")

cardValSum = sum(listOfCardValues)

print(f"The total value of all three packs is {cardValSum}.")
mechanical_meat
  • 163,903
  • 24
  • 228
  • 223
Jcode94
  • 1
  • 1

2 Answers2

1

The problem is with what you think this line will do:

if packValues['sum1'] > packValues['sum2'] or packValues['sum3']:

You should instead do:

if (packValues['sum1'] > packValues['sum2'] or 
    packValues['sum1'] > packValues['sum3']):

Because the first variant will always return True when packValues['sum3'] is not None or when the first condition is met.

And the second variant will return True only when both conditions are met.

mechanical_meat
  • 163,903
  • 24
  • 228
  • 223
0

if packValues['sum1'] > packValues['sum2'] or packValues['sum3']

This line is your problem. Try this:

if 5:
    print("XD")

What happens? Does it print XD? Yes. But why? 5 isn't a boolean...

if "Pancakes":
    print("...but waffles are better...")

Same thing...

if [1,2,3]:
    print(4)

Uh...so anything works that isn't False?

if 0:
    print("nothing")
elif "":
    print("the void")
elif []:
    print("...")

Wait what? Nothing printed...

The reason is anything that is a 0-equivalent is false while anything else is true. So let's go back to your if statement:

if packValues['sum1'] > packValues['sum2'] or packValues['sum3']

Well, the left side of the 'or' is fine, but what is the right side asking?

"Is there such a thing as packValues['sum3']?"

Since their is, the answer is true and anything or True is true, so that code always runs. To compensate, you have to restate your premise which is

...or packValues['sum1'] > packValues['sum3']:
c4llmeco4ch
  • 311
  • 2
  • 11