Reputation: 47
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)
Upvotes: 0
Views: 63
Reputation: 13185
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 print
s, 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)
Upvotes: 1
Reputation: 1912
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
Upvotes: 0