share
Stack OverflowHow do i make this loop if its a tie?
[-4] [2] Bob narly
[2017-10-24 03:28:51]
[ python ]
[ https://stackoverflow.com/questions/46901730/how-do-i-make-this-loop-if-its-a-tie ]

Im trying to make my program loop if both computer and human draw the same card it is my game of war i am developing and if both land on the same card i want the program to loop over and draw more cards until the tie is broke! i got it to a new stage but get an error when i run this on pycharm, not sure

what is wrong this time it looks to be ok, i changed all of the values instead of printing it nows sets the value then prints later on in the program

import random
while True:
cardValue = random.randint(2,14)
cardSuit = random.randint (0,3)
humanCard = random.randint (2,14)
humanSuit = random.randint (0,3)


if cardValue == 14:
    print ("Ace")
elif cardValue == 13:
    print ("King")
elif cardValue == 12:
    print ("Queen")
elif cardValue == 11:
    print ("Jack")
elif cardValue < 10:
    print (cardValue)

if cardSuit == 0:
    print ("Spades")
elif cardSuit == 1:
    print ("Diamonds")
elif cardSuit ==2:
    print ("Hearts")
elif cardSuit == 3:
    print *"Clubs"


if humanCard == 14:
    print ("Ace")
elif humanCard == 13:
    print ("King")
elif humanCard == 12:
    print ("Queen")
elif humanCard == 11:
    print ("Jack")
elif humanCard < 10:
    print (cardValue)

if humanSuit == 0:
    print ("Spades")
elif humanSuit == 1:
    print ("Diamonds")
elif humanSuit == 2:
    print ("Hearts")
elif humanSuit ==3:
    print ("Clubs")


if humanCard > cardValue:
    print("Human Wins!")
    break
elif humanCard < cardValue:
    print('Computer Wins!')
    break
else:
    print ('Its a draw!')
    while True:
        continue
I would suggest encapsulating your code inside a function and then calling it recursively in the humanCard==cardValue case. Return a value from the recursive function if you need to keep track of how many ties you get. - David
(2) Did you even take a look at the tags you put on this question? war - web application archive, tie - perl command. Do you think those tags were applicable to your question? - coldspeed95
(2) You seem to know about while True, so use it around all your code. break when it should stop - OneCricketeer
@David i dont really understand that, what is a function? - Bob narly
[+1] [2017-10-24 03:50:50] André Santos

I would do it this way (requires python 3.6 because of the f-strings on the print's):

import random

suits        = ['Spades', 'Diamonds', 'Hearts', 'Clubs']
cards        = ['2','3','4','5','6','7','8','9','10','Jack','Queen','King','Ace']
cards_values = [2,3,4,5,6,7,8,9,10,11,12,13,14]

map_cards_values = dict(zip(cards,cards_values))
end_game = False

while not end_game:
    aiCard    = random.choice(cards)
    aiSuit    = random.choice(suits)
    humanCard = random.choice(cards)
    humanSuit = random.choice(suits)

    print(f"AI card is a {aiCard} of {aiSuit}")
    print(f"Human card is a {humanCard} of {humanSuit}")

    if map_cards_values[aiCard] != map_cards_values[humanCard]:
        end_game=True

1
[0] [2017-10-24 03:38:09] kgf3JfUtW
  1. I see you are using a while (True) infinite loop. Using an infinite loop is ok. But this code while True: continue will get you stuck forever, because continue simply says go to the next iteration.

  2. Your code that draws the card should be inside the while (True) loop. Since you like new cards to be drawn in each round.


Below is a modified version of your code. Carefully reason through it line-by-line.

import random

while True:
  cardValue = random.randint(2,14)
  cardSuit  = random.randint(0,3)
  humanCard = random.randint(2,14)
  humanSuit = random.randint(0,3)

  # do whatever you do to print the cards

  if humanCard > cardValue:
      print ("Human Wins!")
      break # exit while loop
  elif humanCard < cardValue:
      print ("Computer Wins!")
      break # exit while loop
  else: # they draw same card
      print ("It's a draw!")
      # keep going
      while True:
        continue

wouldn't i want the cardValue = random.randint(2,14) - Bob narly
@Bobnarly You most certainly would. Typo fixed. - kgf3JfUtW
i get this error in python when running my new code TypeError: '>' not supported between instances of 'str' and 'int' in pycharm - Bob narly
@Bob That means you're trying to compare a str (string) with an int (integer) which is not allowed. For example, your variable cardValue is initially an int. But then you do cardValue = "Ace", which changes it to a str. This is not recommended. - kgf3JfUtW
should i revert back to printing them, this seems to be a cleaner output - Bob narly
Sure. So for example, when humanCard is an integer 9, and cardValue is a string 'Jack', the comparison fails because of incompatible types. Try not doing things like if cardValue == 14: cardValue = "Ace". - kgf3JfUtW
ok so i reverted back to the print ill post the new code, but when its a draw it just outputs "Its a draw!" it doesn't replay the game - Bob narly
2