Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import random
- name = input("Please enter your name: ")
- player_score = 0
- computer_score = 0
- player_count = 2
- computer_count = 2
- keepPlaying = True
- deal = 'y'
- def getCard(score) :
- card = random.randint(1,11)
- score = score + card
- return score
- def checkResults(player_score, computer_score) :
- if player_score < 21 and computer_score < 21 :
- return True
- elif player_score > 21 and computer_score < 21 :
- print("You bust! The dealer wins.")
- return False
- elif player_score < 21 and computer_score > 21 :
- print("The dealer busts. You win!")
- return False
- elif player_score == 21 and computer_score < 21 :
- print("Blackjack! You win!")
- return False
- elif computer_score == 21 and player_score < 21 :
- print("Blackjack! The dealer wins.")
- return False
- def displayScore(player_score, computer_score) :
- print("")
- print("~ Score ~")
- print("{0}: {1}".format(name, player_score))
- print("Dealer: ", computer_score)
- player_score = getCard(player_score)
- computer_score = getCard(computer_score)
- player_score = getCard(player_score)
- computer_score = getCard(computer_score)
- displayScore(player_score, computer_score)
- keepPlaying = checkResults(player_score, computer_score)
- while keepPlaying == True and deal == 'y' :
- print("Would you like another card?")
- deal = input("Enter 'y' or 'n': ")
- deal = deal.lower()
- while deal != 'y' and deal != 'n':
- print("Error: Invalid input.")
- deal = input("Enter 'y' or 'n': ")
- if deal == 'y':
- player_score = getCard(player_score)
- player_count = player_count + 1
- displayScore(player_score, computer_score)
- keepPlaying = checkResults(player_score, computer_score)
- elif deal == 'n':
- while computer_score < 16 or computer_score < player_score :
- computer_score = getCard(computer_score)
- computer_count = computer_count + 1
- displayScore(player_score, computer_score)
- keepPlaying = checkResults(player_score, computer_score)
- if player_score < 21 and computer_score < 21 :
- if player_score > computer_score :
- print("You win!")
- elif player_score == computer_score :
- if player_count > computer_count :
- print("Tied score - the dealer wins with less cards.")
- elif computer_count < player_count :
- print("Tied score - you win with less cards!")
- else :
- print("Tie!")
- else :
- print("The dealer wins.")
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement