So far I have this code:
Each if statement in the drawMore() function works individually. Hitting works as intended, displaying the new hand and new total. The else statement works, returning to the top when the user inputs an incorrect input, and the stand logic works in that it breaks out of the loop once the user inputs “s” or “S”. The issue occurs once the function loops again. If I hit the first time the function loops, it returns to the top of the function, but if from there, I decide to stand, it hits another card regardless. Same with the incorrect input statement, where if I hit during the first loop, it works as intended but the second time after I have hit once, inputting an incorrect response also causes it to just hit another card. I cannot figure out why for the life of me. I may be stupid, but shouldn’t the hitOrPass variable change each time I input a new value for it?
import random import math global playerIn playerIn = True dealerIn = True #deck deck = ["2", "3", "4", "5", "6", "7", "8", "9", "10", "J", "Q", "K", "A", "2", "3", "4", "5", "6", "7", "8", "9", "10", "J", "Q", "K", "A", "2", "3", "4", "5", "6", "7", "8", "9", "10", "J", "Q", "K", "A", "2", "3", "4", "5", "6", "7", "8", "9", "10", "J", "Q", "K", "A"] dealerHand = [] #beginning dealer hand [X-0, firstcard-1, secondcard-2] playerHand = [] def dealerCardsDealt (): for x in range(2): card = random.choice(deck) dealerHand.append(card) deck.remove(card) x += 1 def playerCardsDealt (): for x in range (2): card = random.choice(deck) playerHand.append(card) deck.remove(card) x += 1 #calculate player total def playerTotal (): total = 0 for card in playerHand: if card in ["2", "3", "4", "5", "6", "7", "8", "9", "10"]: total += int(card) elif card in ["J", "Q", "K"]: total += 10 elif card in ["A"]: if total > 10: total += 1 elif total <= 10: total += 11 return (total) #calculate dealer total def dealerTotal (): total = 0 for card in dealerHand: if card in ["2", "3", "4", "5", "6", "7", "8", "9", "10"]: total += int(card) elif card in ["J", "Q", "K"]: total += 10 elif card in ["A"]: if total > 10: total += 1 elif total <= 10: total += 11 return (total) dealerCardsDealt() playerCardsDealt() print("") print("Dealer hand is:", "X,", dealerHand[1]) #X- Unknown card, 1- second card #for i in [0, 1]: #print(dealerHand[i], end=" ") print("-----------------------------") print(f"Your hand is:", (playerHand[0]), ",", playerHand[1], "for a total of", (playerTotal())) #0-first card in hand print("-----------------------------") #ask player to hit or pass def drawMore (): hitOrPass = input("Select:nh to hitns to standn") while True: if hitOrPass == "h" or hitOrPass == "H": card = random.choice(deck) playerHand.append(card) deck.remove(card) print("Your hand is:", playerHand, "for a total of:", playerTotal()) drawMore() elif hitOrPass == "s" or hitOrPass == "S": print("You stand.") print("-----------------------------") False break else : print("Please enter a valid input of h or p. ") print("-----------------------------") drawMore() drawMore() print("@3 STOOD END OF TURN") #dealer hit logic
submitted by /u/PrankPatrolOfficial
[link] [comments]
r/learnpython So far I have this code: Each if statement in the drawMore() function works individually. Hitting works as intended, displaying the new hand and new total. The else statement works, returning to the top when the user inputs an incorrect input, and the stand logic works in that it breaks out of the loop once the user inputs “s” or “S”. The issue occurs once the function loops again. If I hit the first time the function loops, it returns to the top of the function, but if from there, I decide to stand, it hits another card regardless. Same with the incorrect input statement, where if I hit during the first loop, it works as intended but the second time after I have hit once, inputting an incorrect response also causes it to just hit another card. I cannot figure out why for the life of me. I may be stupid, but shouldn’t the hitOrPass variable change each time I input a new value for it? import random import math global playerIn playerIn = True dealerIn = True #deck deck = [“2”, “3”, “4”, “5”, “6”, “7”, “8”, “9”, “10”, “J”, “Q”, “K”, “A”, “2”, “3”, “4”, “5”, “6”, “7”, “8”, “9”, “10”, “J”, “Q”, “K”, “A”, “2”, “3”, “4”, “5”, “6”, “7”, “8”, “9”, “10”, “J”, “Q”, “K”, “A”, “2”, “3”, “4”, “5”, “6”, “7”, “8”, “9”, “10”, “J”, “Q”, “K”, “A”] dealerHand = [] #beginning dealer hand [X-0, firstcard-1, secondcard-2] playerHand = [] def dealerCardsDealt (): for x in range(2): card = random.choice(deck) dealerHand.append(card) deck.remove(card) x += 1 def playerCardsDealt (): for x in range (2): card = random.choice(deck) playerHand.append(card) deck.remove(card) x += 1 #calculate player total def playerTotal (): total = 0 for card in playerHand: if card in [“2”, “3”, “4”, “5”, “6”, “7”, “8”, “9”, “10”]: total += int(card) elif card in [“J”, “Q”, “K”]: total += 10 elif card in [“A”]: if total > 10: total += 1 elif total <= 10: total += 11 return (total) #calculate dealer total def dealerTotal (): total = 0 for card in dealerHand: if card in [“2”, “3”, “4”, “5”, “6”, “7”, “8”, “9”, “10”]: total += int(card) elif card in [“J”, “Q”, “K”]: total += 10 elif card in [“A”]: if total > 10: total += 1 elif total <= 10: total += 11 return (total) dealerCardsDealt() playerCardsDealt() print(“”) print(“Dealer hand is:”, “X,”, dealerHand[1]) #X- Unknown card, 1- second card #for i in [0, 1]: #print(dealerHand[i], end=” “) print(“—————————–“) print(f”Your hand is:”, (playerHand[0]), “,”, playerHand[1], “for a total of”, (playerTotal())) #0-first card in hand print(“—————————–“) #ask player to hit or pass def drawMore (): hitOrPass = input(“Select:nh to hitns to standn”) while True: if hitOrPass == “h” or hitOrPass == “H”: card = random.choice(deck) playerHand.append(card) deck.remove(card) print(“Your hand is:”, playerHand, “for a total of:”, playerTotal()) drawMore() elif hitOrPass == “s” or hitOrPass == “S”: print(“You stand.”) print(“—————————–“) False break else : print(“Please enter a valid input of h or p. “) print(“—————————–“) drawMore() drawMore() print(“@3 STOOD END OF TURN”) #dealer hit logic submitted by /u/PrankPatrolOfficial [link] [comments]
So far I have this code:
Each if statement in the drawMore() function works individually. Hitting works as intended, displaying the new hand and new total. The else statement works, returning to the top when the user inputs an incorrect input, and the stand logic works in that it breaks out of the loop once the user inputs “s” or “S”. The issue occurs once the function loops again. If I hit the first time the function loops, it returns to the top of the function, but if from there, I decide to stand, it hits another card regardless. Same with the incorrect input statement, where if I hit during the first loop, it works as intended but the second time after I have hit once, inputting an incorrect response also causes it to just hit another card. I cannot figure out why for the life of me. I may be stupid, but shouldn’t the hitOrPass variable change each time I input a new value for it?
import random import math global playerIn playerIn = True dealerIn = True #deck deck = ["2", "3", "4", "5", "6", "7", "8", "9", "10", "J", "Q", "K", "A", "2", "3", "4", "5", "6", "7", "8", "9", "10", "J", "Q", "K", "A", "2", "3", "4", "5", "6", "7", "8", "9", "10", "J", "Q", "K", "A", "2", "3", "4", "5", "6", "7", "8", "9", "10", "J", "Q", "K", "A"] dealerHand = [] #beginning dealer hand [X-0, firstcard-1, secondcard-2] playerHand = [] def dealerCardsDealt (): for x in range(2): card = random.choice(deck) dealerHand.append(card) deck.remove(card) x += 1 def playerCardsDealt (): for x in range (2): card = random.choice(deck) playerHand.append(card) deck.remove(card) x += 1 #calculate player total def playerTotal (): total = 0 for card in playerHand: if card in ["2", "3", "4", "5", "6", "7", "8", "9", "10"]: total += int(card) elif card in ["J", "Q", "K"]: total += 10 elif card in ["A"]: if total > 10: total += 1 elif total <= 10: total += 11 return (total) #calculate dealer total def dealerTotal (): total = 0 for card in dealerHand: if card in ["2", "3", "4", "5", "6", "7", "8", "9", "10"]: total += int(card) elif card in ["J", "Q", "K"]: total += 10 elif card in ["A"]: if total > 10: total += 1 elif total <= 10: total += 11 return (total) dealerCardsDealt() playerCardsDealt() print("") print("Dealer hand is:", "X,", dealerHand[1]) #X- Unknown card, 1- second card #for i in [0, 1]: #print(dealerHand[i], end=" ") print("-----------------------------") print(f"Your hand is:", (playerHand[0]), ",", playerHand[1], "for a total of", (playerTotal())) #0-first card in hand print("-----------------------------") #ask player to hit or pass def drawMore (): hitOrPass = input("Select:nh to hitns to standn") while True: if hitOrPass == "h" or hitOrPass == "H": card = random.choice(deck) playerHand.append(card) deck.remove(card) print("Your hand is:", playerHand, "for a total of:", playerTotal()) drawMore() elif hitOrPass == "s" or hitOrPass == "S": print("You stand.") print("-----------------------------") False break else : print("Please enter a valid input of h or p. ") print("-----------------------------") drawMore() drawMore() print("@3 STOOD END OF TURN") #dealer hit logic
submitted by /u/PrankPatrolOfficial
[link] [comments]