This is my blackjack python script. I was having an issue and I think it is a problem with the concatenation of the hit function. I have tried looking online and reading other questions but couldn't find one that applied. I am not sure if there are other problems with this script and if you find any please share. Thanks for any help if you can give any. This is my code:
import random
#run = True is a setup for a later use in a while true loop
run = True
#prints out a welcome
print("Welcome to blackjack in Python, hope you enjoy")
print("Side note: All Ace's are treated with a value of one")
#This generates all the cards and since their are 13 card not counting suites
dealer_showing = random.randint(1, 13)
dealer_hiding = random.randint(1, 13)
player_1st_card = random.randint(1, 13)
player_2nd_card = random.randint(1, 13)
dealers_cards = dealer_hiding + dealer_showing
player_cards = player_1st_card + player_2nd_card
#Their is no card worth 11, 12, or 13 points in blackjack so this gives all "face cards" a value of 10
def check_13(who_to_check):
if who_to_check >= 11:
who_to_check = 10
return who_to_check
#This uses the function seen up above
check_13(dealer_showing)
check_13(player_1st_card)
check_13(player_2nd_card)
check_13(dealer_hiding)
#This tells the user what their card value is and what the user is showing
print("Your card value is {}".format(player_cards))
print("The dealer is showing a {}".format(dealer_showing))
# Here I put in a function that checks if the player or computer have busted or won
def check_cards():
if player_cards > 21 and dealers_cards > 21:
print("Both you and the computer have busted")
run = False
elif player_cards > 21:
print("I'm sorry the computer has won")
run = False
elif dealers_cards > 21:
print("The computer has busted you win")
run = False
elif player_cards == 21 and dealers_cards == 21:
print("Both you and the dealer have won")
elif player_cards == 21:
print("You have brought down the house")
elif dealers_cards == 21:
print("I'm sorry the computer has won")
# Here I put in a function that hits
def hit():
player_cards = player_cards + check_13(random.randint(1, 10))
return player_cards
print("Your card value is now {}".format(player_cards))
# Here I put in a function that lets the player stay
def stay():
if player_cards > dealers_cards:
print("Congratulations you have won and beat the Computer")
run = False
elif player_cards == dealers_cards:
print("You and the Computer have tied")
else:
print("I'm sorry the computer has won")
False
#Here is what the program runs over and over again in this loop
while run == True:
check_cards()
print(player_cards)
input_hit = input("Would you like to hit y/n: ")
if input_hit == "y":
hit()
elif input_hit == "n":
stay()
Aucun commentaire:
Enregistrer un commentaire