TIL: Python Basics Day 11 - Blackjack Capstone Project

이다연·2020년 12월 10일
0

Udemy Python Course

목록 보기
11/64

Blackjack Capstone Project

Drawio for flowchart

Beginning of a project, I was stressed that I wasn't able to make a proper requirement list and draw a flow chart. Considering that I haven't played it before and I don't enjoy gaming (even though it's a simple black jack probability game) It's normal that figuring out the hidden logic behind creating a game will be definitely tricky.
Important thing is, learning from it. I learnt how to use website for drawing a flow chart during this struggle. When I need a fancy flow chart, I can do so very easily.
Flow chart isn't easy either. Good thing is, I am getting used to it. Mutiple ifs, while loops, conditions, I already know the syntax. Don't worry too much even before you start it. Enjoy the process, as long as you keep trying, you will get there. Like... there!

random. sample vs choice vs randint

-sample returns a random [list] (from the sequence)
-choice returns an random item from an already existing list object
-randint returns an integer within the range which is given when used

  • sample
    sample() is an inbuilt function of random module in Python that returns a particular length list of items chosen from the sequence i.e. list, tuple, string or set. Used for random sampling without replacement.

    Syntax : random.sample(sequence, k)
    
    Parameters:
    sequence: Can be a list, tuple, string, or set.
    k: An Integer value, it specify the length of a sample.
    
    Returns: k length new list of elements chosen from the sequence.
  • randint
    random.randint(a, b) is used for getting a random number between two given numbers
    e.g. if I want a number between 1 and 50 I would do random.randint(1, 50)

For loop in range()

for _ in range(2):
-> you don't always need an existing list for 'for loop'. You can use 'range(start, stop)' to give the condition, how many times to iterate the execution part below.
range(2) also means 0 to number-1. In this case, 0, 1 -> 2 times

#Hint 5: Deal the user and computer 2 cards each using deal_card() and append().
user_cards =[]
user_cards.append(deal_card())
user_cards.append(deal_card())

print(f"user: {user_cards}")

computer_cards = []
computer_cards.append(deal_card())
computer_cards.append(deal_card())

print(f"computer: {computer_cards}")

#--------------------------<ANGELA'S>------------------------------
user_cards =[]
computer_cards = []
for card in range(2): #remember that range(2) menas var.card => 0, 1 -> loop through it two times 
  user_cards.append(deal_card()) 
  computer_cards.append(deal_card()) 

extend & += ,: extend a list by appending, it has to be a 'list' itself.
append is used when you want to add a single item, not a list.

#Hint 7: Inside calculate_score() check for a blackjack (a hand with only 2 cards: ace + 10) and return 0 instead of the actual score. 0 will represent a blackjack in our game.

def calculate_score(list_of_cards):
  
  total_score = sum(list_of_cards)
  if total_score == 21:
    return 0 #blackjack
  elif total_score <21:
    return total_score 
  elif 11 in list_of_cards and total_score > 21:
    list_of_cards.remove(11)
    list_of_cards.append(1)
    return total_score 
  elif total_score > 21:
    return total_score
    

#------------angela's ---------
def calculate_score(cards):
  if sum(cards) == 21 and len(cards) == 2:
    return 0
  if 11 in cards and sum(cards) > 21:
    cards.remove(11)
    cards.append(1)

restart (while, for loop outside & if inside)

Restart 1: while loop, outside the function. To restart a game, it needs to be in a loop. By using "function inside while loop", it can iterate itself.
The process of repeatedly applying the same function is called iteration.
Note: while input == 'y' ,
when boolean is True -> call the function. It has to have a condition.

def play_game():
   user_cards = []
    #.
    #.
   return print("1")
    
while input("Do you want to play a game of BlackJack? Type 'y' or 'n': ") == 'y':
	clear()
	play_game() #-> called inside while loop for the first time and will repeat itself from here

Restart 2: recursion inside its own function. And has to call the function outside of function, end of the line to execute it.
Function calls itself within the function. It has to have a flag : "if statement" otherwise goes into an infinite loop.

def play_game():
  user_cards = []
    #.
    #.
  return print("1")
    
  print(compare(user_score, computer_score))    
  if input("start y?") == 'y':
    play_game()
  
play_game() # -> got to be typed at first, after this first call, function will be called inside function itself


.

profile
Dayeon Lee | Django & Python Web Developer

0개의 댓글