Welcome to the Get Help category!
This is where you can ask questions about your code. Some important things to remember when posting in this category
- Learn how to ask a good question and get a good answer!
- Remember to include a link to the exercise you need help with!
- If someone answers your question, please mark their response as a solution
- Once you understand a new concept, come back and try to help someone else!
Hello, so I am completing the python terminal game project in the computer science track, I have decided to try and create a simple game of blackjack against the computer, although I am having some issues with the logic around deciding who has won the game. For some reason it is inconsistent at getting it correct, and so far I can’t figure out why. If anyone can have a read and let me know I would really appreciate it! Here is my program:
#This is the main window for my blackjack game
from random import randint
print(“Welcome to blackjack! Would you like to see the rules?”)
user_see_rules = input("yes or no: ")
if user_see_rules == “yes”:
print(“”“You will play against the dealer to see who can get the closest number to 21.
You will receive two random numbers, as will the dealer.
At this point you are able to choose whether you hit or stand.
To hit means you will receive another number, as you aim to get closer to 21.
To stand means you are satisfied with your current numbers.
If the total of your numbers goes over 21 you will go bust and the dealer will win.
Otherwise, the winner is whoevers numbers totalled together is closest to 21.
Good luck!”“”)
user_total = 0
dealer_total = 0
user_gone_bust = False
dealer_gone_bust = False
#Assigning cards
user_num1 = randint(1, 11)
user_num2 = randint(1, 10)
dealer_num1 = randint(1, 11)
dealer_num2 = randint(1, 10)
user_total = user_num1 + user_num2
dealer_total = dealer_num1 + dealer_num2
print(user_num1, user_num2)
Give user the option to take another card
def hit_or_stand(user_total):
hit_or_stand = input("hit or stand: ")
user_num3 = 0
if hit_or_stand == “hit”:
user_num3 = randint(1, 10)
print(user_num1, user_num2, user_num3)
user_total += user_num3
if user_total > 21:
user_gone_bust = True
return “You have gone bust!”
else:
return "Your score is " + str(user_total)
def dealer_hit_or_stand_and_bust(dealer_total):
dealer_num3 = 0
if randint(0, 1) == 1:
dealer_num3 = randint(1, 10)
dealer_total += dealer_num3
if dealer_total > 21:
dealer_gone_bust = True
return “The dealer has gone bust!”
else:
return "The dealer’s score is " + str(dealer_total)
def check_if_bust(user_gone_bust, dealer_gone_bust):
if user_gone_bust == True:
return “You have gone bust! Dealer wins”
if dealer_gone_bust == True:
return “The computer has gone bust! You win”
else:
return “And the winner is…”
def decide_a_winner(user_total, dealer_total):
if user_gone_bust == False and dealer_gone_bust == False:
if user_total > dealer_total:
return “Congratulations! You have won”
elif dealer_total > user_total:
return “The computer, unlucky!”
else:
return “Wow! A draw”
print(hit_or_stand(user_total))
print(dealer_hit_or_stand_and_bust(dealer_total))
print(check_if_bust(user_gone_bust, dealer_gone_bust))
print(decide_a_winner(user_total, dealer_total))