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 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
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.
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
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
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
war- web application archive,tie- perl command. Do you think those tags were applicable to your question? - coldspeed95while True, so use it around all your code.breakwhen it should stop - OneCricketeer