0

In the below program the result of the if statement near the end is occasionally wrong but I cannot see a pattern in it. I have debugged it as much as possible but I cannot find my error. I'm sorry for the large amount of code but I don't know where the error is in that code all I know is that it is somewhere in the code below.

Help would be much appreciated!

I am not hugely experienced in python so please explain any answers in detail

for i in range(1,10):
import random

Cards = list(range(0,8))
print("shuffling...")
random.shuffle(Cards)
#print(Cards)

PlayerCards = []
ComputerCards = []
CardsLen = int(len(Cards)) / 2

for i in range(int(CardsLen)):
    PlayerCards.append(Cards[0])
    del Cards[0]
    ComputerCards.append(Cards[0])
    del Cards[0]

#print(PlayerCards)
#print(ComputerCards)



Ab5s = ['9', '12', '1', '10', '5', '6', '6', '8']

#print(Ab5s)

Ab5sS = sorted(Ab5s, key = int)
#print(Ab5sS)


AbNames = ['Magic', 'Cunning', 'Courage', 'Wisdom', 'Temper']
#print(AbNames)

Ab5Name = AbNames[4]
AbNamesDict = {5: Ab5s}



PlayerCardNum = PlayerCards[0]
ComputerCardNum = ComputerCards[0]



PlayerCardAb5 = Ab5s[PlayerCardNum]
ComputerCardAb5 = Ab5s[ComputerCardNum]


PlayerAbVal = Ab5s[PlayerCardNum]
ComputerAbVal = Ab5s[ComputerCardNum]

#print(PlayerAbVal)
#print(ComputerAbVal)


while True:
    try:
        PlayerAbUse = "Temper" #input("Which ability do you want to use ")
        PlayerAbUse = PlayerAbUse.title()
        PlayerAbNum = (AbNames.index(PlayerAbUse) + 1)
        break

    except ValueError:
        print("error")
        continue


AbList = AbNamesDict[PlayerAbNum]    
PlayerAbVal = (AbList)[PlayerCardNum]
ComputerAbVal = (AbList)[ComputerCardNum]

#print(PlayerAbVal)
#print(ComputerAbVal)

print("Player has " + PlayerCardAb5 + " " + Ab5Name)
print("Computer has " + ComputerAbVal + " " + Ab5Name)

WonCards = []


#This is where I believe it is going wrong

if PlayerAbVal < ComputerAbVal:
    Winner = 0
else:
    if PlayerAbVal == ComputerAbVal:
        Winner = 2
    else:
        Winner = 1

WonCards.append(PlayerCards.pop(0))
WonCards.append(ComputerCards.pop(0))



if Winner == 0:
    print("Computer Win ")
    while len(WonCards) > 0:
        ComputerCards.append(WonCards.pop(0))
    print("")
else:
    if Winner == 1:
        print("Player Win ")
        while len(WonCards) > 0:
            PlayerCards.append(WonCards.pop(0))
        print("")

    else:
        print("Draw ")
        Winner = random.randint(1,2)
  • 2
    What exactly is this code supposed to do? What am I looking for? It's better if you isolate the code that you suspect is giving problems :-) – Mangohero1 Aug 07 '17 at 19:25
  • 3
    `for i in range(1,10): import random`. What? – roganjosh Aug 07 '17 at 19:27
  • @mangoHero1 The code is part of a top trumps game and sometimes it give the wrong answer for the winner – user8430559 Aug 07 '17 at 19:33
  • Could you please provide full traceback of the error so we can better help you. – Professor_Joykill Aug 07 '17 at 19:37
  • Is this Python 2? Your list `Ab5sS` contains strings. Python 2 will allow you to compare `int` and `str` with `>` etc but the output is nonsensical compared to what you want. Get rid of the quotes on each number in `Ab5sS`. The fact that it is sometimes right is just coincidental. – roganjosh Aug 07 '17 at 19:39
  • python doesnt give me an error message but its result is wrong – user8430559 Aug 07 '17 at 19:39
  • @roganjosh the print statements are followed by parentheses, so this is python3 – Mangohero1 Aug 07 '17 at 19:40
  • @mangoHero1 that in itself does not provide anything conclusive `print('something')` is also valid in Python 2. – roganjosh Aug 07 '17 at 19:41
  • @roganjosh this is python 3, sorry i forgot to say – user8430559 Aug 07 '17 at 19:42
  • In that case I'm out of ideas. It's not easy to follow your code (names like `Ab5sS` and similar variations aren't helpful for understanding), you should try and boil this down to an isolated issue. In doing that, you might find the answer yourself. – roganjosh Aug 07 '17 at 19:43
  • If you run the code and look at the results sometimes when Computer has it higher than the player it says the player wins and vice versa – user8430559 Aug 07 '17 at 19:43
  • I'm not convinced you are running Python 3. Please put `import sys` at the top of your file and then have `print(sys.version)`. Copy/paste that output back here. If I convert those strings to ints, it works fine for me. In other words, I can replicate your issue perfectly in Python 2 and it's easily fixed. **Regardless**; there's no reason to have those numbers as strings, you can solve the concatenation problem in your `prints` by casting them to strings there or using `format()` (which is the better way) – roganjosh Aug 07 '17 at 19:49
  • @roganjosh 3.6.1 (v3.6.1:69c0db5, Mar 21 2017, 17:54:52) [MSC v.1900 32 bit (Intel)] – user8430559 Aug 07 '17 at 19:53
  • @roganjosh that appears to have worked! Thanks alot if you post it as an answer I will mark it as accepted. Also, how do you use format() – user8430559 Aug 07 '17 at 19:59

2 Answers2

1

Your issue is that you are comparing strings, not numerical values. My initial thought was that you were in Python 2, but actually this failure can also be explained by lexicographical ordering

Change your list Ab5sS to contain integers. So:

Ab5s = [9, 12, 1, 10, 5, 6, 6, 8]

To fix your prints, cast to string there e.g.:

print("Player has " + str(PlayerCardAb5) + " " + Ab5Name)

Or, better, use format():

print("Player has {} {}".format(PlayerCardAb5, Ab5Name))

As you mentioned in another comment, your issue is that you're reading strings as values from a file. This doesn't stop you from converting the values to int; the easiest way is probably just a list comprehension:

Ab5s = ['9', '12', '1', '10', '5', '6', '6', '8']
Ab5s = [int(item) for item in Ab5s]

There's other ways, such as map, but I think that's falling out of favour.

Ab5s = map(float, Ab5s)
roganjosh
  • 12,594
  • 4
  • 29
  • 46
0

Just modify your if statement to force the type to be int's. (use if..elif..else for more pythonicness)

if int(PlayerAbVal) < int(ComputerAbVal):
   Winner = 0
elif int(PlayerAbVal) == int(ComputerAbVal):
   Winner = 2
else:
   Winner = 1
Mangohero1
  • 1,832
  • 2
  • 12
  • 20
  • Thanks, I was going to accept @roganjosh 's solution but this is better for me as it allows me to import the list from a file – user8430559 Aug 07 '17 at 20:07
  • This does work but there is no reason to have the list as strings in the first place, so it seems counter-intuitive to me to work around that fact. – roganjosh Aug 07 '17 at 20:08
  • @user8430559 ok, that wasn't actually in your question. Had I known that, I would have shown you the quick conversion. I will edit my answer, regardless of which you choose to accept, because you shouldn't really be coding around this issue rather than addressing it at source. – roganjosh Aug 07 '17 at 20:09
  • sorry @roganjosh I was attempting to simplify what I was posting, I admit I didn't do a very good job – user8430559 Aug 07 '17 at 20:18
  • @user8430559 it's fine; asking your first question is daunting :) I added the edit to my answer anyway so you can see how to do the conversion. – roganjosh Aug 07 '17 at 20:21