Xin chào, tôi là người mới trong Python. Tôi theo một khóa đào tạo về python ở Udemy, và một trong những dự án là trò chơi blackjack. Tôi thấy mã này đơn giản, nhưng cảm thấy hơi quá đơn giản. Thay vì hiển thị thẻ, nó chỉ hiển thị giá trị thẻ. Tôi đã thực hiện một số chỉnh sửa ở đây và ở đó, sử dụng đệ quy và nó bị hỏng một chút
Tôi đã ghi lại một số vấn đề tôi gặp phải trong phần nhận xét trong #. — —. mẫu. Có thể do mình là newbie nên nhai hoài mà không nuốt nổi.
Xin vui lòng làm sáng tỏ cho tôi.
import random
# ---- INITIALIZATION ----
# Cards
the_card = {
"2" : 2,
"3" : 3,
"4" : 4,
"5" : 5,
"6" : 6,
"7" : 7,
"8" : 8,
"9" : 9,
"10": 10,
"J" : 10,
"Q" : 10,
"K" : 10,
"A" : 11,
}
# Initial account
initial = int[input["How many dollars you bring to the table? $"]]
# ---- Functions ----
# ---- Betting function ----
def bet[acc]:
''' Betting function '''
while True:
your_bet = int[input["How many you willing to bet? $"]]
if your_bet > acc:
print["Your bet is higher than your account. Please repeat all."]
continue
else:
return your_bet
# ---- Draw cards ----
def dealing[cards]:
''' Dealing card function. Return card lists and their value. '''
print["Game start."]
your_card = [random.choice[list[cards.keys[]]] for i in range[0, 2]]
your_val = sum[cards[i] for i in your_card]
dealer_card = [random.choice[list[cards.keys[]]] for i in range[0, 2]]
dealer_val = sum[cards[i] for i in dealer_card]
return your_card, your_val, dealer_card, dealer_val
# ---- Adding card ----
def add_card[hand, cards]:
'''Draw another card '''
new_card = random.choice[list[cards.keys[]]]
hand.append[new_card]
new_val = sum[cards[i] for i in hand]
return hand, new_val
# ---- Compare hands ----
# !--- open_hand[] returns None ---!
def open_hand[hand, hand_val, dealer_hand, dealer_val, acc, bet, cards]:
''' Checking both hands' cards value. Return user money amount.'''
if dealer_val 21:
# Will be triggered if
acc += bet
print[f'Your hand is {hand} with value {hand_val}.\nDealer cards are{dealer_hand} with value {dealer_val}.']
print[f"You win! Your money now is ${acc}."]
return acc
elif hand_val < 21 and dealer_val < 21:
# Triggered when both hands are under 21.
# Whoever have higher hand, wins.
if hand_val < dealer_val:
acc -= bet
print[f'Your hand is {hand} with value {hand_val}.\nDealer cards are{dealer_hand} with value {dealer_val}.']
print[f"You lose. Your money now is ${acc}."]
return acc
elif hand_val > dealer_val:
acc += bet
print[f'Your hand is {hand} with value {hand_val}.\nDealer cards are{dealer_hand} with value {dealer_val}.']
print[f"You win! Your money now is ${acc}."]
return acc
elif hand_val == dealer_val:
print[f'Your hand is {hand} with value {hand_val}.\nDealer cards are{dealer_hand} with value {dealer_val}.']
print["Both hands are same. It's draw."]
return acc
# --- Control if user will stop or continue.
def if_continue[money, cards]:
still_play = input["Do you want to play again? Press y to continue, or any other keys if you want to stop: > "].lower[]
if still_play == "y":
game_21[money, cards, status=True]
else:
# !--- After end_game[] excecuted, the program is not terminated ---!
# !--- It's back to game_21[] ---!
end_game[money]
# ---- End game ----
def end_game[money]:
return f"Game over, your money is ${money}."
# ---- The body of program ----
# ---- Combining all other functions ----
# ---- Indirect recursion to game_21[] from if_continue[] ----
def game_21[money, cards, status]:
''' Core game program.'''
put_bet = bet[money]
print["Let's begin the game."]
hand, hand_val, dealer_hand, dealer_val = dealing[cards]
print[f"Your hand is {hand} with value {hand_val}.\nDealer's first card is {dealer_hand[0]} with value {cards[dealer_hand[0]]}."]
# Need one more recursive here.
# ! --- The loop become endless ---!
while status:
if hand_val < 21:
draw_more = input["Do you want to draw again? Press y to continue, or any other keys if you want to stand: > "].lower[]
if draw_more =="y":
hand, hand_val = add_card[hand, cards]
print[f"Your hand is {hand} with value {hand_val}.\nDealer's first card is {dealer_hand[0]} with value {cards[dealer_hand[0]]}."]
continue
else:
# !--- PROBLEM: open_hand[] return None ---!
money = open_hand[hand, hand_val, dealer_hand, dealer_val, money, put_bet, cards]
if_continue[money, cards]
elif hand_val == 21:
money += put_bet
print["Blackjack!"]
print[f"Your money now is ${money}."]
if_continue[money, cards]
else:
money -= put_bet
print[f"Your hand value is over than 21. You lose ${put_bet}. Your money now is ${money}."]
if_continue[money, cards]
status = False
# ---- PROGRAM STARTS HERE ----
game_21[initial, the_card, status = True]
# !--- Error raised ---!
# !--- TypeError: '>' not supported between instances of 'int' and 'NoneType' ---!
# !--- Help T_T ---!
Cảm ơn sự giúp đỡ của bạn, folks
Xin chào @yafethtb, hơi khó đọc mã của bạn, nhưng chỉ cần thêm hai dòng trống giữa mỗi chức năng [theo PEP 8, tiêu chuẩn cho kiểu mã Python] sẽ giúp đọc dễ dàng hơn nhiều. Ngoài ra, chỉ cần FYI, theo tiêu chuẩn chuỗi tài liệu [PEP 257], chuỗi tài liệu phải luôn sử dụng ba dấu ngoặc kép [
acc = open_hand[...]
return acc
1], thay vì ba dấu ngoặc đơn [acc = open_hand[...]
return acc
2] và không có dấu cách xung quanh chúngVới những điều tầm thường đó, hãy chuyển sang các vấn đề của bạn
Đầu tiên, trong Python, nếu thân hàm kết thúc thực thi mà không có câu lệnh trả về rõ ràng, thì hàm đó sẽ trả về
acc = open_hand[...]
return acc
3. Nếu chúng ta xem xét tất cả các nhánh của acc = open_hand[...]
return acc
4, chúng ta có thể thấy rằng tất cả chúng đều là acc = open_hand[...]
return acc
5, có lẽ phải là một số…tất cả trừ một trong số chúng, tức là nhánh đầu tiên; . Để khắc phục điều này, chỉ cần yêu cầu nó trả về giá trị trả về là acc = open_hand[...]
return acc
4, tôi. e. acc = open_hand[...]
return acc
3. Nếu bạn thích, để giống với những cái khác, bạn cũng có thể làmacc = open_hand[...]
return acc
Điều này sửa lỗi
acc = open_hand[...]
return acc
4 trả về acc = open_hand[...]
return acc
3 và kết quả là acc = open_hand[...]
return acc
6 [vì acc = open_hand[...]
return acc
7 trong hàm cha được đặt thành acc = open_hand[...]
return acc
3, tất nhiên bạn không thể thêm các giá trị số vào]Tuy nhiên, chúng tôi vẫn có một vấn đề, như bạn đã lưu ý. nếu chúng tôi gõ
acc = open_hand[...]
return acc
9 để chơi một trò chơi khác, mọi thứ sẽ hoạt động, nhưng nếu chúng tôi không làm như vậy, trò chơi ban đầu sẽ tiếp tục, với việc người chia bài tiếp tục rút bài và bạn tiếp tục thắng, bất kể bạn gõ gì. Một lần nữa, chúng tôi theo dõi mã để xem điều gì đang xảy ra. Chúng ta có thể thấy rằng sau khi acc = open_hand[...]
return acc
6 được gọi bởi ________ 51, sau đó ________ 52 được gọi. Người dùng được hỏi liệu họ có còn muốn chơi hay không; . Nhưng nếu không, import random
# Set SEED to None for a random game, or to an integer for a deterministic one
SEED = None
random.seed[SEED]
# ---- INITIALIZATION ----
# Cards
the_card = {
"2" : 2,
"3" : 3,
"4" : 4,
"5" : 5,
"6" : 6,
"7" : 7,
"8" : 8,
"9" : 9,
"10": 10,
"J" : 10,
"Q" : 10,
"K" : 10,
"A" : 11,
}
# Initial account
initial = int[input["How many dollars you bring to the table? $"]]
# ---- Functions ----
# ---- Betting function ----
def bet[acc]:
''' Betting function '''
while True:
your_bet = int[input["How many you willing to bet? $"]]
if your_bet > acc:
print["Your bet is higher than your account. Please repeat all."]
continue
else:
return your_bet
# ---- Draw cards ----
def dealing[cards]:
''' Dealing card function. Return card lists and their value. '''
print["Game start."]
your_card = [random.choice[list[cards.keys[]]] for i in range[0, 2]]
your_val = sum[cards[i] for i in your_card]
dealer_card = [random.choice[list[cards.keys[]]] for i in range[0, 2]]
dealer_val = sum[cards[i] for i in dealer_card]
return your_card, your_val, dealer_card, dealer_val
# ---- Adding card ----
def add_card[hand, cards]:
'''Draw another card '''
new_card = random.choice[list[cards.keys[]]]
hand.append[new_card]
new_val = sum[cards[i] for i in hand]
return hand, new_val
# ---- Compare hands ----
def open_hand[hand, hand_val, dealer_hand, dealer_val, acc, bet, cards]:
''' Checking both hands' cards value. Return user money amount.'''
if dealer_val 21:
# Will be triggered if
acc += bet
print[f'Your hand is {hand} with value {hand_val}.\nDealer cards are{dealer_hand} with value {dealer_val}.']
print[f"You win! Your money now is ${acc}."]
elif hand_val < 21 and dealer_val < 21:
# Triggered when both hands are under 21.
# Whoever have higher hand, wins.
if hand_val < dealer_val:
acc -= bet
print[f'Your hand is {hand} with value {hand_val}.\nDealer cards are{dealer_hand} with value {dealer_val}.']
print[f"You lose. Your money now is ${acc}."]
elif hand_val > dealer_val:
acc += bet
print[f'Your hand is {hand} with value {hand_val}.\nDealer cards are{dealer_hand} with value {dealer_val}.']
print[f"You win! Your money now is ${acc}."]
elif hand_val == dealer_val:
print[f'Your hand is {hand} with value {hand_val}.\nDealer cards are{dealer_hand} with value {dealer_val}.']
print["Both hands are same. It's draw."]
return acc
# --- Control if user will stop or continue.
def if_continue[money, cards]:
still_play = input["Do you want to play again? Press y to continue, or any other keys if you want to stop: > "].lower[]
if still_play == "y":
game_21[money, cards, status=True]
else:
end_game[money]
# ---- End game ----
def end_game[money]:
print[f"Game over, your money is ${money}."]
# ---- The body of program ----
# ---- Combining all other functions ----
# ---- Indirect recursion to game_21[] from if_continue[] ----
def game_21[money, cards, status]:
''' Core game program.'''
put_bet = bet[money]
print["Let's begin the game."]
hand, hand_val, dealer_hand, dealer_val = dealing[cards]
print[f"Your hand is {hand} with value {hand_val}.\nDealer's first card is {dealer_hand[0]} with value {cards[dealer_hand[0]]}."]
# Need one more recursive here.
while status:
if hand_val < 21:
draw_more = input["Do you want to draw again? Press y to continue, or any other keys if you want to stand: > "].lower[]
if draw_more =="y":
hand, hand_val = add_card[hand, cards]
print[f"Your hand is {hand} with value {hand_val}.\nDealer's first card is {dealer_hand[0]} with value {cards[dealer_hand[0]]}."]
continue
else:
money = open_hand[hand, hand_val, dealer_hand, dealer_val, money, put_bet, cards]
if_continue[money, cards]
elif hand_val == 21:
money += put_bet
print["Blackjack!"]
print[f"Your money now is ${money}."]
if_continue[money, cards]
else:
money -= put_bet
print[f"Your hand value is over than 21. You lose ${put_bet}. Your money now is ${money}."]
if_continue[money, cards]
status = False
# ---- PROGRAM STARTS HERE ----
game_21[initial, the_card, status = True]
5 được gọi, và rắc rối của chúng ta bắt đầu từ đâyThay vào đó, hàm
import random
# Set SEED to None for a random game, or to an integer for a deterministic one
SEED = None
random.seed[SEED]
# ---- INITIALIZATION ----
# Cards
the_card = {
"2" : 2,
"3" : 3,
"4" : 4,
"5" : 5,
"6" : 6,
"7" : 7,
"8" : 8,
"9" : 9,
"10": 10,
"J" : 10,
"Q" : 10,
"K" : 10,
"A" : 11,
}
# Initial account
initial = int[input["How many dollars you bring to the table? $"]]
# ---- Functions ----
# ---- Betting function ----
def bet[acc]:
''' Betting function '''
while True:
your_bet = int[input["How many you willing to bet? $"]]
if your_bet > acc:
print["Your bet is higher than your account. Please repeat all."]
continue
else:
return your_bet
# ---- Draw cards ----
def dealing[cards]:
''' Dealing card function. Return card lists and their value. '''
print["Game start."]
your_card = [random.choice[list[cards.keys[]]] for i in range[0, 2]]
your_val = sum[cards[i] for i in your_card]
dealer_card = [random.choice[list[cards.keys[]]] for i in range[0, 2]]
dealer_val = sum[cards[i] for i in dealer_card]
return your_card, your_val, dealer_card, dealer_val
# ---- Adding card ----
def add_card[hand, cards]:
'''Draw another card '''
new_card = random.choice[list[cards.keys[]]]
hand.append[new_card]
new_val = sum[cards[i] for i in hand]
return hand, new_val
# ---- Compare hands ----
def open_hand[hand, hand_val, dealer_hand, dealer_val, acc, bet, cards]:
''' Checking both hands' cards value. Return user money amount.'''
if dealer_val 21:
# Will be triggered if
acc += bet
print[f'Your hand is {hand} with value {hand_val}.\nDealer cards are{dealer_hand} with value {dealer_val}.']
print[f"You win! Your money now is ${acc}."]
elif hand_val < 21 and dealer_val < 21:
# Triggered when both hands are under 21.
# Whoever have higher hand, wins.
if hand_val < dealer_val:
acc -= bet
print[f'Your hand is {hand} with value {hand_val}.\nDealer cards are{dealer_hand} with value {dealer_val}.']
print[f"You lose. Your money now is ${acc}."]
elif hand_val > dealer_val:
acc += bet
print[f'Your hand is {hand} with value {hand_val}.\nDealer cards are{dealer_hand} with value {dealer_val}.']
print[f"You win! Your money now is ${acc}."]
elif hand_val == dealer_val:
print[f'Your hand is {hand} with value {hand_val}.\nDealer cards are{dealer_hand} with value {dealer_val}.']
print["Both hands are same. It's draw."]
return acc
# --- Control if user will stop or continue.
def if_continue[money, cards]:
still_play = input["Do you want to play again? Press y to continue, or any other keys if you want to stop: > "].lower[]
if still_play == "y":
game_21[money, cards, status=True]
else:
end_game[money]
# ---- End game ----
def end_game[money]:
print[f"Game over, your money is ${money}."]
# ---- The body of program ----
# ---- Combining all other functions ----
# ---- Indirect recursion to game_21[] from if_continue[] ----
def game_21[money, cards, status]:
''' Core game program.'''
put_bet = bet[money]
print["Let's begin the game."]
hand, hand_val, dealer_hand, dealer_val = dealing[cards]
print[f"Your hand is {hand} with value {hand_val}.\nDealer's first card is {dealer_hand[0]} with value {cards[dealer_hand[0]]}."]
# Need one more recursive here.
while status:
if hand_val < 21:
draw_more = input["Do you want to draw again? Press y to continue, or any other keys if you want to stand: > "].lower[]
if draw_more =="y":
hand, hand_val = add_card[hand, cards]
print[f"Your hand is {hand} with value {hand_val}.\nDealer's first card is {dealer_hand[0]} with value {cards[dealer_hand[0]]}."]
continue
else:
money = open_hand[hand, hand_val, dealer_hand, dealer_val, money, put_bet, cards]
if_continue[money, cards]
elif hand_val == 21:
money += put_bet
print["Blackjack!"]
print[f"Your money now is ${money}."]
if_continue[money, cards]
else:
money -= put_bet
print[f"Your hand value is over than 21. You lose ${put_bet}. Your money now is ${money}."]
if_continue[money, cards]
status = False
# ---- PROGRAM STARTS HERE ----
game_21[initial, the_card, status = True]
5 trả về một chuỗi trông giống như nó có nghĩa là import random
# Set SEED to None for a random game, or to an integer for a deterministic one
SEED = None
random.seed[SEED]
# ---- INITIALIZATION ----
# Cards
the_card = {
"2" : 2,
"3" : 3,
"4" : 4,
"5" : 5,
"6" : 6,
"7" : 7,
"8" : 8,
"9" : 9,
"10": 10,
"J" : 10,
"Q" : 10,
"K" : 10,
"A" : 11,
}
# Initial account
initial = int[input["How many dollars you bring to the table? $"]]
# ---- Functions ----
# ---- Betting function ----
def bet[acc]:
''' Betting function '''
while True:
your_bet = int[input["How many you willing to bet? $"]]
if your_bet > acc:
print["Your bet is higher than your account. Please repeat all."]
continue
else:
return your_bet
# ---- Draw cards ----
def dealing[cards]:
''' Dealing card function. Return card lists and their value. '''
print["Game start."]
your_card = [random.choice[list[cards.keys[]]] for i in range[0, 2]]
your_val = sum[cards[i] for i in your_card]
dealer_card = [random.choice[list[cards.keys[]]] for i in range[0, 2]]
dealer_val = sum[cards[i] for i in dealer_card]
return your_card, your_val, dealer_card, dealer_val
# ---- Adding card ----
def add_card[hand, cards]:
'''Draw another card '''
new_card = random.choice[list[cards.keys[]]]
hand.append[new_card]
new_val = sum[cards[i] for i in hand]
return hand, new_val
# ---- Compare hands ----
def open_hand[hand, hand_val, dealer_hand, dealer_val, acc, bet, cards]:
''' Checking both hands' cards value. Return user money amount.'''
if dealer_val 21:
# Will be triggered if
acc += bet
print[f'Your hand is {hand} with value {hand_val}.\nDealer cards are{dealer_hand} with value {dealer_val}.']
print[f"You win! Your money now is ${acc}."]
elif hand_val < 21 and dealer_val < 21:
# Triggered when both hands are under 21.
# Whoever have higher hand, wins.
if hand_val < dealer_val:
acc -= bet
print[f'Your hand is {hand} with value {hand_val}.\nDealer cards are{dealer_hand} with value {dealer_val}.']
print[f"You lose. Your money now is ${acc}."]
elif hand_val > dealer_val:
acc += bet
print[f'Your hand is {hand} with value {hand_val}.\nDealer cards are{dealer_hand} with value {dealer_val}.']
print[f"You win! Your money now is ${acc}."]
elif hand_val == dealer_val:
print[f'Your hand is {hand} with value {hand_val}.\nDealer cards are{dealer_hand} with value {dealer_val}.']
print["Both hands are same. It's draw."]
return acc
# --- Control if user will stop or continue.
def if_continue[money, cards]:
still_play = input["Do you want to play again? Press y to continue, or any other keys if you want to stop: > "].lower[]
if still_play == "y":
game_21[money, cards, status=True]
else:
end_game[money]
# ---- End game ----
def end_game[money]:
print[f"Game over, your money is ${money}."]
# ---- The body of program ----
# ---- Combining all other functions ----
# ---- Indirect recursion to game_21[] from if_continue[] ----
def game_21[money, cards, status]:
''' Core game program.'''
put_bet = bet[money]
print["Let's begin the game."]
hand, hand_val, dealer_hand, dealer_val = dealing[cards]
print[f"Your hand is {hand} with value {hand_val}.\nDealer's first card is {dealer_hand[0]} with value {cards[dealer_hand[0]]}."]
# Need one more recursive here.
while status:
if hand_val < 21:
draw_more = input["Do you want to draw again? Press y to continue, or any other keys if you want to stand: > "].lower[]
if draw_more =="y":
hand, hand_val = add_card[hand, cards]
print[f"Your hand is {hand} with value {hand_val}.\nDealer's first card is {dealer_hand[0]} with value {cards[dealer_hand[0]]}."]
continue
else:
money = open_hand[hand, hand_val, dealer_hand, dealer_val, money, put_bet, cards]
if_continue[money, cards]
elif hand_val == 21:
money += put_bet
print["Blackjack!"]
print[f"Your money now is ${money}."]
if_continue[money, cards]
else:
money -= put_bet
print[f"Your hand value is over than 21. You lose ${put_bet}. Your money now is ${money}."]
if_continue[money, cards]
status = False
# ---- PROGRAM STARTS HERE ----
game_21[initial, the_card, status = True]
7ed. Sau đó, trở lại bên trong của import random
# Set SEED to None for a random game, or to an integer for a deterministic one
SEED = None
random.seed[SEED]
# ---- INITIALIZATION ----
# Cards
the_card = {
"2" : 2,
"3" : 3,
"4" : 4,
"5" : 5,
"6" : 6,
"7" : 7,
"8" : 8,
"9" : 9,
"10": 10,
"J" : 10,
"Q" : 10,
"K" : 10,
"A" : 11,
}
# Initial account
initial = int[input["How many dollars you bring to the table? $"]]
# ---- Functions ----
# ---- Betting function ----
def bet[acc]:
''' Betting function '''
while True:
your_bet = int[input["How many you willing to bet? $"]]
if your_bet > acc:
print["Your bet is higher than your account. Please repeat all."]
continue
else:
return your_bet
# ---- Draw cards ----
def dealing[cards]:
''' Dealing card function. Return card lists and their value. '''
print["Game start."]
your_card = [random.choice[list[cards.keys[]]] for i in range[0, 2]]
your_val = sum[cards[i] for i in your_card]
dealer_card = [random.choice[list[cards.keys[]]] for i in range[0, 2]]
dealer_val = sum[cards[i] for i in dealer_card]
return your_card, your_val, dealer_card, dealer_val
# ---- Adding card ----
def add_card[hand, cards]:
'''Draw another card '''
new_card = random.choice[list[cards.keys[]]]
hand.append[new_card]
new_val = sum[cards[i] for i in hand]
return hand, new_val
# ---- Compare hands ----
def open_hand[hand, hand_val, dealer_hand, dealer_val, acc, bet, cards]:
''' Checking both hands' cards value. Return user money amount.'''
if dealer_val 21:
# Will be triggered if
acc += bet
print[f'Your hand is {hand} with value {hand_val}.\nDealer cards are{dealer_hand} with value {dealer_val}.']
print[f"You win! Your money now is ${acc}."]
elif hand_val < 21 and dealer_val < 21:
# Triggered when both hands are under 21.
# Whoever have higher hand, wins.
if hand_val < dealer_val:
acc -= bet
print[f'Your hand is {hand} with value {hand_val}.\nDealer cards are{dealer_hand} with value {dealer_val}.']
print[f"You lose. Your money now is ${acc}."]
elif hand_val > dealer_val:
acc += bet
print[f'Your hand is {hand} with value {hand_val}.\nDealer cards are{dealer_hand} with value {dealer_val}.']
print[f"You win! Your money now is ${acc}."]
elif hand_val == dealer_val:
print[f'Your hand is {hand} with value {hand_val}.\nDealer cards are{dealer_hand} with value {dealer_val}.']
print["Both hands are same. It's draw."]
return acc
# --- Control if user will stop or continue.
def if_continue[money, cards]:
still_play = input["Do you want to play again? Press y to continue, or any other keys if you want to stop: > "].lower[]
if still_play == "y":
game_21[money, cards, status=True]
else:
end_game[money]
# ---- End game ----
def end_game[money]:
print[f"Game over, your money is ${money}."]
# ---- The body of program ----
# ---- Combining all other functions ----
# ---- Indirect recursion to game_21[] from if_continue[] ----
def game_21[money, cards, status]:
''' Core game program.'''
put_bet = bet[money]
print["Let's begin the game."]
hand, hand_val, dealer_hand, dealer_val = dealing[cards]
print[f"Your hand is {hand} with value {hand_val}.\nDealer's first card is {dealer_hand[0]} with value {cards[dealer_hand[0]]}."]
# Need one more recursive here.
while status:
if hand_val < 21:
draw_more = input["Do you want to draw again? Press y to continue, or any other keys if you want to stand: > "].lower[]
if draw_more =="y":
hand, hand_val = add_card[hand, cards]
print[f"Your hand is {hand} with value {hand_val}.\nDealer's first card is {dealer_hand[0]} with value {cards[dealer_hand[0]]}."]
continue
else:
money = open_hand[hand, hand_val, dealer_hand, dealer_val, money, put_bet, cards]
if_continue[money, cards]
elif hand_val == 21:
money += put_bet
print["Blackjack!"]
print[f"Your money now is ${money}."]
if_continue[money, cards]
else:
money -= put_bet
print[f"Your hand value is over than 21. You lose ${put_bet}. Your money now is ${money}."]
if_continue[money, cards]
status = False
# ---- PROGRAM STARTS HERE ----
game_21[initial, the_card, status = True]
2, chuỗi đó, giá trị trả về của import random
# Set SEED to None for a random game, or to an integer for a deterministic one
SEED = None
random.seed[SEED]
# ---- INITIALIZATION ----
# Cards
the_card = {
"2" : 2,
"3" : 3,
"4" : 4,
"5" : 5,
"6" : 6,
"7" : 7,
"8" : 8,
"9" : 9,
"10": 10,
"J" : 10,
"Q" : 10,
"K" : 10,
"A" : 11,
}
# Initial account
initial = int[input["How many dollars you bring to the table? $"]]
# ---- Functions ----
# ---- Betting function ----
def bet[acc]:
''' Betting function '''
while True:
your_bet = int[input["How many you willing to bet? $"]]
if your_bet > acc:
print["Your bet is higher than your account. Please repeat all."]
continue
else:
return your_bet
# ---- Draw cards ----
def dealing[cards]:
''' Dealing card function. Return card lists and their value. '''
print["Game start."]
your_card = [random.choice[list[cards.keys[]]] for i in range[0, 2]]
your_val = sum[cards[i] for i in your_card]
dealer_card = [random.choice[list[cards.keys[]]] for i in range[0, 2]]
dealer_val = sum[cards[i] for i in dealer_card]
return your_card, your_val, dealer_card, dealer_val
# ---- Adding card ----
def add_card[hand, cards]:
'''Draw another card '''
new_card = random.choice[list[cards.keys[]]]
hand.append[new_card]
new_val = sum[cards[i] for i in hand]
return hand, new_val
# ---- Compare hands ----
def open_hand[hand, hand_val, dealer_hand, dealer_val, acc, bet, cards]:
''' Checking both hands' cards value. Return user money amount.'''
if dealer_val 21:
# Will be triggered if
acc += bet
print[f'Your hand is {hand} with value {hand_val}.\nDealer cards are{dealer_hand} with value {dealer_val}.']
print[f"You win! Your money now is ${acc}."]
elif hand_val < 21 and dealer_val < 21:
# Triggered when both hands are under 21.
# Whoever have higher hand, wins.
if hand_val < dealer_val:
acc -= bet
print[f'Your hand is {hand} with value {hand_val}.\nDealer cards are{dealer_hand} with value {dealer_val}.']
print[f"You lose. Your money now is ${acc}."]
elif hand_val > dealer_val:
acc += bet
print[f'Your hand is {hand} with value {hand_val}.\nDealer cards are{dealer_hand} with value {dealer_val}.']
print[f"You win! Your money now is ${acc}."]
elif hand_val == dealer_val:
print[f'Your hand is {hand} with value {hand_val}.\nDealer cards are{dealer_hand} with value {dealer_val}.']
print["Both hands are same. It's draw."]
return acc
# --- Control if user will stop or continue.
def if_continue[money, cards]:
still_play = input["Do you want to play again? Press y to continue, or any other keys if you want to stop: > "].lower[]
if still_play == "y":
game_21[money, cards, status=True]
else:
end_game[money]
# ---- End game ----
def end_game[money]:
print[f"Game over, your money is ${money}."]
# ---- The body of program ----
# ---- Combining all other functions ----
# ---- Indirect recursion to game_21[] from if_continue[] ----
def game_21[money, cards, status]:
''' Core game program.'''
put_bet = bet[money]
print["Let's begin the game."]
hand, hand_val, dealer_hand, dealer_val = dealing[cards]
print[f"Your hand is {hand} with value {hand_val}.\nDealer's first card is {dealer_hand[0]} with value {cards[dealer_hand[0]]}."]
# Need one more recursive here.
while status:
if hand_val < 21:
draw_more = input["Do you want to draw again? Press y to continue, or any other keys if you want to stand: > "].lower[]
if draw_more =="y":
hand, hand_val = add_card[hand, cards]
print[f"Your hand is {hand} with value {hand_val}.\nDealer's first card is {dealer_hand[0]} with value {cards[dealer_hand[0]]}."]
continue
else:
money = open_hand[hand, hand_val, dealer_hand, dealer_val, money, put_bet, cards]
if_continue[money, cards]
elif hand_val == 21:
money += put_bet
print["Blackjack!"]
print[f"Your money now is ${money}."]
if_continue[money, cards]
else:
money -= put_bet
print[f"Your hand value is over than 21. You lose ${put_bet}. Your money now is ${money}."]
if_continue[money, cards]
status = False
# ---- PROGRAM STARTS HERE ----
game_21[initial, the_card, status = True]
5, không được gán cho bất kỳ thứ gì hoặc được trả về, vì vậy nó không có hiệu lực. Vì không có câu lệnh acc = open_hand[...]
return acc
10 trong nhánh đó, nên import random
# Set SEED to None for a random game, or to an integer for a deterministic one
SEED = None
random.seed[SEED]
# ---- INITIALIZATION ----
# Cards
the_card = {
"2" : 2,
"3" : 3,
"4" : 4,
"5" : 5,
"6" : 6,
"7" : 7,
"8" : 8,
"9" : 9,
"10": 10,
"J" : 10,
"Q" : 10,
"K" : 10,
"A" : 11,
}
# Initial account
initial = int[input["How many dollars you bring to the table? $"]]
# ---- Functions ----
# ---- Betting function ----
def bet[acc]:
''' Betting function '''
while True:
your_bet = int[input["How many you willing to bet? $"]]
if your_bet > acc:
print["Your bet is higher than your account. Please repeat all."]
continue
else:
return your_bet
# ---- Draw cards ----
def dealing[cards]:
''' Dealing card function. Return card lists and their value. '''
print["Game start."]
your_card = [random.choice[list[cards.keys[]]] for i in range[0, 2]]
your_val = sum[cards[i] for i in your_card]
dealer_card = [random.choice[list[cards.keys[]]] for i in range[0, 2]]
dealer_val = sum[cards[i] for i in dealer_card]
return your_card, your_val, dealer_card, dealer_val
# ---- Adding card ----
def add_card[hand, cards]:
'''Draw another card '''
new_card = random.choice[list[cards.keys[]]]
hand.append[new_card]
new_val = sum[cards[i] for i in hand]
return hand, new_val
# ---- Compare hands ----
def open_hand[hand, hand_val, dealer_hand, dealer_val, acc, bet, cards]:
''' Checking both hands' cards value. Return user money amount.'''
if dealer_val 21:
# Will be triggered if
acc += bet
print[f'Your hand is {hand} with value {hand_val}.\nDealer cards are{dealer_hand} with value {dealer_val}.']
print[f"You win! Your money now is ${acc}."]
elif hand_val < 21 and dealer_val < 21:
# Triggered when both hands are under 21.
# Whoever have higher hand, wins.
if hand_val < dealer_val:
acc -= bet
print[f'Your hand is {hand} with value {hand_val}.\nDealer cards are{dealer_hand} with value {dealer_val}.']
print[f"You lose. Your money now is ${acc}."]
elif hand_val > dealer_val:
acc += bet
print[f'Your hand is {hand} with value {hand_val}.\nDealer cards are{dealer_hand} with value {dealer_val}.']
print[f"You win! Your money now is ${acc}."]
elif hand_val == dealer_val:
print[f'Your hand is {hand} with value {hand_val}.\nDealer cards are{dealer_hand} with value {dealer_val}.']
print["Both hands are same. It's draw."]
return acc
# --- Control if user will stop or continue.
def if_continue[money, cards]:
still_play = input["Do you want to play again? Press y to continue, or any other keys if you want to stop: > "].lower[]
if still_play == "y":
game_21[money, cards, status=True]
else:
end_game[money]
# ---- End game ----
def end_game[money]:
print[f"Game over, your money is ${money}."]
# ---- The body of program ----
# ---- Combining all other functions ----
# ---- Indirect recursion to game_21[] from if_continue[] ----
def game_21[money, cards, status]:
''' Core game program.'''
put_bet = bet[money]
print["Let's begin the game."]
hand, hand_val, dealer_hand, dealer_val = dealing[cards]
print[f"Your hand is {hand} with value {hand_val}.\nDealer's first card is {dealer_hand[0]} with value {cards[dealer_hand[0]]}."]
# Need one more recursive here.
while status:
if hand_val < 21:
draw_more = input["Do you want to draw again? Press y to continue, or any other keys if you want to stand: > "].lower[]
if draw_more =="y":
hand, hand_val = add_card[hand, cards]
print[f"Your hand is {hand} with value {hand_val}.\nDealer's first card is {dealer_hand[0]} with value {cards[dealer_hand[0]]}."]
continue
else:
money = open_hand[hand, hand_val, dealer_hand, dealer_val, money, put_bet, cards]
if_continue[money, cards]
elif hand_val == 21:
money += put_bet
print["Blackjack!"]
print[f"Your money now is ${money}."]
if_continue[money, cards]
else:
money -= put_bet
print[f"Your hand value is over than 21. You lose ${put_bet}. Your money now is ${money}."]
if_continue[money, cards]
status = False
# ---- PROGRAM STARTS HERE ----
game_21[initial, the_card, status = True]
2 trả về acc = open_hand[...]
return acc
3. Bây giờ chúng ta quay lại nơi mà import random
# Set SEED to None for a random game, or to an integer for a deterministic one
SEED = None
random.seed[SEED]
# ---- INITIALIZATION ----
# Cards
the_card = {
"2" : 2,
"3" : 3,
"4" : 4,
"5" : 5,
"6" : 6,
"7" : 7,
"8" : 8,
"9" : 9,
"10": 10,
"J" : 10,
"Q" : 10,
"K" : 10,
"A" : 11,
}
# Initial account
initial = int[input["How many dollars you bring to the table? $"]]
# ---- Functions ----
# ---- Betting function ----
def bet[acc]:
''' Betting function '''
while True:
your_bet = int[input["How many you willing to bet? $"]]
if your_bet > acc:
print["Your bet is higher than your account. Please repeat all."]
continue
else:
return your_bet
# ---- Draw cards ----
def dealing[cards]:
''' Dealing card function. Return card lists and their value. '''
print["Game start."]
your_card = [random.choice[list[cards.keys[]]] for i in range[0, 2]]
your_val = sum[cards[i] for i in your_card]
dealer_card = [random.choice[list[cards.keys[]]] for i in range[0, 2]]
dealer_val = sum[cards[i] for i in dealer_card]
return your_card, your_val, dealer_card, dealer_val
# ---- Adding card ----
def add_card[hand, cards]:
'''Draw another card '''
new_card = random.choice[list[cards.keys[]]]
hand.append[new_card]
new_val = sum[cards[i] for i in hand]
return hand, new_val
# ---- Compare hands ----
def open_hand[hand, hand_val, dealer_hand, dealer_val, acc, bet, cards]:
''' Checking both hands' cards value. Return user money amount.'''
if dealer_val 21:
# Will be triggered if
acc += bet
print[f'Your hand is {hand} with value {hand_val}.\nDealer cards are{dealer_hand} with value {dealer_val}.']
print[f"You win! Your money now is ${acc}."]
elif hand_val < 21 and dealer_val < 21:
# Triggered when both hands are under 21.
# Whoever have higher hand, wins.
if hand_val < dealer_val:
acc -= bet
print[f'Your hand is {hand} with value {hand_val}.\nDealer cards are{dealer_hand} with value {dealer_val}.']
print[f"You lose. Your money now is ${acc}."]
elif hand_val > dealer_val:
acc += bet
print[f'Your hand is {hand} with value {hand_val}.\nDealer cards are{dealer_hand} with value {dealer_val}.']
print[f"You win! Your money now is ${acc}."]
elif hand_val == dealer_val:
print[f'Your hand is {hand} with value {hand_val}.\nDealer cards are{dealer_hand} with value {dealer_val}.']
print["Both hands are same. It's draw."]
return acc
# --- Control if user will stop or continue.
def if_continue[money, cards]:
still_play = input["Do you want to play again? Press y to continue, or any other keys if you want to stop: > "].lower[]
if still_play == "y":
game_21[money, cards, status=True]
else:
end_game[money]
# ---- End game ----
def end_game[money]:
print[f"Game over, your money is ${money}."]
# ---- The body of program ----
# ---- Combining all other functions ----
# ---- Indirect recursion to game_21[] from if_continue[] ----
def game_21[money, cards, status]:
''' Core game program.'''
put_bet = bet[money]
print["Let's begin the game."]
hand, hand_val, dealer_hand, dealer_val = dealing[cards]
print[f"Your hand is {hand} with value {hand_val}.\nDealer's first card is {dealer_hand[0]} with value {cards[dealer_hand[0]]}."]
# Need one more recursive here.
while status:
if hand_val < 21:
draw_more = input["Do you want to draw again? Press y to continue, or any other keys if you want to stand: > "].lower[]
if draw_more =="y":
hand, hand_val = add_card[hand, cards]
print[f"Your hand is {hand} with value {hand_val}.\nDealer's first card is {dealer_hand[0]} with value {cards[dealer_hand[0]]}."]
continue
else:
money = open_hand[hand, hand_val, dealer_hand, dealer_val, money, put_bet, cards]
if_continue[money, cards]
elif hand_val == 21:
money += put_bet
print["Blackjack!"]
print[f"Your money now is ${money}."]
if_continue[money, cards]
else:
money -= put_bet
print[f"Your hand value is over than 21. You lose ${put_bet}. Your money now is ${money}."]
if_continue[money, cards]
status = False
# ---- PROGRAM STARTS HERE ----
game_21[initial, the_card, status = True]
2 được gọi là…nhưng bản thân giá trị của nó bị loại bỏ và acc = open_hand[...]
return acc
14 không được sửa đổi. Vì trạng thái không bao giờ được đặt thành acc = open_hand[...]
return acc
15 trong nhánh này, nên vòng lặp import random
# Set SEED to None for a random game, or to an integer for a deterministic one
SEED = None
random.seed[SEED]
# ---- INITIALIZATION ----
# Cards
the_card = {
"2" : 2,
"3" : 3,
"4" : 4,
"5" : 5,
"6" : 6,
"7" : 7,
"8" : 8,
"9" : 9,
"10": 10,
"J" : 10,
"Q" : 10,
"K" : 10,
"A" : 11,
}
# Initial account
initial = int[input["How many dollars you bring to the table? $"]]
# ---- Functions ----
# ---- Betting function ----
def bet[acc]:
''' Betting function '''
while True:
your_bet = int[input["How many you willing to bet? $"]]
if your_bet > acc:
print["Your bet is higher than your account. Please repeat all."]
continue
else:
return your_bet
# ---- Draw cards ----
def dealing[cards]:
''' Dealing card function. Return card lists and their value. '''
print["Game start."]
your_card = [random.choice[list[cards.keys[]]] for i in range[0, 2]]
your_val = sum[cards[i] for i in your_card]
dealer_card = [random.choice[list[cards.keys[]]] for i in range[0, 2]]
dealer_val = sum[cards[i] for i in dealer_card]
return your_card, your_val, dealer_card, dealer_val
# ---- Adding card ----
def add_card[hand, cards]:
'''Draw another card '''
new_card = random.choice[list[cards.keys[]]]
hand.append[new_card]
new_val = sum[cards[i] for i in hand]
return hand, new_val
# ---- Compare hands ----
def open_hand[hand, hand_val, dealer_hand, dealer_val, acc, bet, cards]:
''' Checking both hands' cards value. Return user money amount.'''
if dealer_val 21:
# Will be triggered if
acc += bet
print[f'Your hand is {hand} with value {hand_val}.\nDealer cards are{dealer_hand} with value {dealer_val}.']
print[f"You win! Your money now is ${acc}."]
elif hand_val < 21 and dealer_val < 21:
# Triggered when both hands are under 21.
# Whoever have higher hand, wins.
if hand_val < dealer_val:
acc -= bet
print[f'Your hand is {hand} with value {hand_val}.\nDealer cards are{dealer_hand} with value {dealer_val}.']
print[f"You lose. Your money now is ${acc}."]
elif hand_val > dealer_val:
acc += bet
print[f'Your hand is {hand} with value {hand_val}.\nDealer cards are{dealer_hand} with value {dealer_val}.']
print[f"You win! Your money now is ${acc}."]
elif hand_val == dealer_val:
print[f'Your hand is {hand} with value {hand_val}.\nDealer cards are{dealer_hand} with value {dealer_val}.']
print["Both hands are same. It's draw."]
return acc
# --- Control if user will stop or continue.
def if_continue[money, cards]:
still_play = input["Do you want to play again? Press y to continue, or any other keys if you want to stop: > "].lower[]
if still_play == "y":
game_21[money, cards, status=True]
else:
end_game[money]
# ---- End game ----
def end_game[money]:
print[f"Game over, your money is ${money}."]
# ---- The body of program ----
# ---- Combining all other functions ----
# ---- Indirect recursion to game_21[] from if_continue[] ----
def game_21[money, cards, status]:
''' Core game program.'''
put_bet = bet[money]
print["Let's begin the game."]
hand, hand_val, dealer_hand, dealer_val = dealing[cards]
print[f"Your hand is {hand} with value {hand_val}.\nDealer's first card is {dealer_hand[0]} with value {cards[dealer_hand[0]]}."]
# Need one more recursive here.
while status:
if hand_val < 21:
draw_more = input["Do you want to draw again? Press y to continue, or any other keys if you want to stand: > "].lower[]
if draw_more =="y":
hand, hand_val = add_card[hand, cards]
print[f"Your hand is {hand} with value {hand_val}.\nDealer's first card is {dealer_hand[0]} with value {cards[dealer_hand[0]]}."]
continue
else:
money = open_hand[hand, hand_val, dealer_hand, dealer_val, money, put_bet, cards]
if_continue[money, cards]
elif hand_val == 21:
money += put_bet
print["Blackjack!"]
print[f"Your money now is ${money}."]
if_continue[money, cards]
else:
money -= put_bet
print[f"Your hand value is over than 21. You lose ${put_bet}. Your money now is ${money}."]
if_continue[money, cards]
status = False
# ---- PROGRAM STARTS HERE ----
game_21[initial, the_card, status = True]
1 ban đầu tiếp tục vô thời hạn, trừ khi bạn chọn đánh [hòa] và bài của bạn bán thân [i. e. nó trên 21], trong trường hợp đó, codepath đó có acc = open_hand[...]
return acc
17, phá vỡ vòng lặpTôi đoán rằng bạn định đánh
acc = open_hand[...]
return acc
17 cuối cùng đó ở cấp độ ngay dưới vòng lặp acc = open_hand[...]
return acc
19, thay vì ở cấp độ của nhánh acc = open_hand[...]
return acc
20, vì vậy nó sẽ chạy bất cứ khi nào tay bạn không dưới 21 và bạn không chọn đánh . Đó là một lỗi phổ biến, thậm chí đôi khi xảy ra với các lập trình viên có kinh nghiệm. Vì vậy, để khắc phục sự cố này, bạn sẽ di chuyển acc = open_hand[...]
return acc
17 lên một mức thụt lề và bây giờ mọi thứ hoạt động. Một tinh chỉnh cuối cùng. như ngụ ý trước đó, bạn có thể muốn thực hiện in thư thay vì trả lại [tôi. e. acc = open_hand[...]
return acc
23], để người dùng thực sự nhìn thấy nóCần lưu ý, thiết kế đệ quy thực sự không phải là cách tốt nhất cho việc này, vì nếu bạn chơi một số lượng lớn trò chơi, các chức năng tiếp tục được thêm vào ngăn xếp và cuối cùng nó sẽ đi sâu quá nhiều cấp độ, dẫn đến
acc = open_hand[...]
return acc
24. Một thiết kế tốt hơn, nếu bạn thực sự triển khai điều này, sẽ là sử dụng vòng lặp acc = open_hand[...]
return acc
19 bên ngoài xử lý import random
# Set SEED to None for a random game, or to an integer for a deterministic one
SEED = None
random.seed[SEED]
# ---- INITIALIZATION ----
# Cards
the_card = {
"2" : 2,
"3" : 3,
"4" : 4,
"5" : 5,
"6" : 6,
"7" : 7,
"8" : 8,
"9" : 9,
"10": 10,
"J" : 10,
"Q" : 10,
"K" : 10,
"A" : 11,
}
# Initial account
initial = int[input["How many dollars you bring to the table? $"]]
# ---- Functions ----
# ---- Betting function ----
def bet[acc]:
''' Betting function '''
while True:
your_bet = int[input["How many you willing to bet? $"]]
if your_bet > acc:
print["Your bet is higher than your account. Please repeat all."]
continue
else:
return your_bet
# ---- Draw cards ----
def dealing[cards]:
''' Dealing card function. Return card lists and their value. '''
print["Game start."]
your_card = [random.choice[list[cards.keys[]]] for i in range[0, 2]]
your_val = sum[cards[i] for i in your_card]
dealer_card = [random.choice[list[cards.keys[]]] for i in range[0, 2]]
dealer_val = sum[cards[i] for i in dealer_card]
return your_card, your_val, dealer_card, dealer_val
# ---- Adding card ----
def add_card[hand, cards]:
'''Draw another card '''
new_card = random.choice[list[cards.keys[]]]
hand.append[new_card]
new_val = sum[cards[i] for i in hand]
return hand, new_val
# ---- Compare hands ----
def open_hand[hand, hand_val, dealer_hand, dealer_val, acc, bet, cards]:
''' Checking both hands' cards value. Return user money amount.'''
if dealer_val 21:
# Will be triggered if
acc += bet
print[f'Your hand is {hand} with value {hand_val}.\nDealer cards are{dealer_hand} with value {dealer_val}.']
print[f"You win! Your money now is ${acc}."]
elif hand_val < 21 and dealer_val < 21:
# Triggered when both hands are under 21.
# Whoever have higher hand, wins.
if hand_val < dealer_val:
acc -= bet
print[f'Your hand is {hand} with value {hand_val}.\nDealer cards are{dealer_hand} with value {dealer_val}.']
print[f"You lose. Your money now is ${acc}."]
elif hand_val > dealer_val:
acc += bet
print[f'Your hand is {hand} with value {hand_val}.\nDealer cards are{dealer_hand} with value {dealer_val}.']
print[f"You win! Your money now is ${acc}."]
elif hand_val == dealer_val:
print[f'Your hand is {hand} with value {hand_val}.\nDealer cards are{dealer_hand} with value {dealer_val}.']
print["Both hands are same. It's draw."]
return acc
# --- Control if user will stop or continue.
def if_continue[money, cards]:
still_play = input["Do you want to play again? Press y to continue, or any other keys if you want to stop: > "].lower[]
if still_play == "y":
game_21[money, cards, status=True]
else:
end_game[money]
# ---- End game ----
def end_game[money]:
print[f"Game over, your money is ${money}."]
# ---- The body of program ----
# ---- Combining all other functions ----
# ---- Indirect recursion to game_21[] from if_continue[] ----
def game_21[money, cards, status]:
''' Core game program.'''
put_bet = bet[money]
print["Let's begin the game."]
hand, hand_val, dealer_hand, dealer_val = dealing[cards]
print[f"Your hand is {hand} with value {hand_val}.\nDealer's first card is {dealer_hand[0]} with value {cards[dealer_hand[0]]}."]
# Need one more recursive here.
while status:
if hand_val < 21:
draw_more = input["Do you want to draw again? Press y to continue, or any other keys if you want to stand: > "].lower[]
if draw_more =="y":
hand, hand_val = add_card[hand, cards]
print[f"Your hand is {hand} with value {hand_val}.\nDealer's first card is {dealer_hand[0]} with value {cards[dealer_hand[0]]}."]
continue
else:
money = open_hand[hand, hand_val, dealer_hand, dealer_val, money, put_bet, cards]
if_continue[money, cards]
elif hand_val == 21:
money += put_bet
print["Blackjack!"]
print[f"Your money now is ${money}."]
if_continue[money, cards]
else:
money -= put_bet
print[f"Your hand value is over than 21. You lose ${put_bet}. Your money now is ${money}."]
if_continue[money, cards]
status = False
# ---- PROGRAM STARTS HERE ----
game_21[initial, the_card, status = True]
2 và gọi hàm import random
# Set SEED to None for a random game, or to an integer for a deterministic one
SEED = None
random.seed[SEED]
# ---- INITIALIZATION ----
# Cards
the_card = {
"2" : 2,
"3" : 3,
"4" : 4,
"5" : 5,
"6" : 6,
"7" : 7,
"8" : 8,
"9" : 9,
"10": 10,
"J" : 10,
"Q" : 10,
"K" : 10,
"A" : 11,
}
# Initial account
initial = int[input["How many dollars you bring to the table? $"]]
# ---- Functions ----
# ---- Betting function ----
def bet[acc]:
''' Betting function '''
while True:
your_bet = int[input["How many you willing to bet? $"]]
if your_bet > acc:
print["Your bet is higher than your account. Please repeat all."]
continue
else:
return your_bet
# ---- Draw cards ----
def dealing[cards]:
''' Dealing card function. Return card lists and their value. '''
print["Game start."]
your_card = [random.choice[list[cards.keys[]]] for i in range[0, 2]]
your_val = sum[cards[i] for i in your_card]
dealer_card = [random.choice[list[cards.keys[]]] for i in range[0, 2]]
dealer_val = sum[cards[i] for i in dealer_card]
return your_card, your_val, dealer_card, dealer_val
# ---- Adding card ----
def add_card[hand, cards]:
'''Draw another card '''
new_card = random.choice[list[cards.keys[]]]
hand.append[new_card]
new_val = sum[cards[i] for i in hand]
return hand, new_val
# ---- Compare hands ----
def open_hand[hand, hand_val, dealer_hand, dealer_val, acc, bet, cards]:
''' Checking both hands' cards value. Return user money amount.'''
if dealer_val 21:
# Will be triggered if
acc += bet
print[f'Your hand is {hand} with value {hand_val}.\nDealer cards are{dealer_hand} with value {dealer_val}.']
print[f"You win! Your money now is ${acc}."]
elif hand_val < 21 and dealer_val < 21:
# Triggered when both hands are under 21.
# Whoever have higher hand, wins.
if hand_val < dealer_val:
acc -= bet
print[f'Your hand is {hand} with value {hand_val}.\nDealer cards are{dealer_hand} with value {dealer_val}.']
print[f"You lose. Your money now is ${acc}."]
elif hand_val > dealer_val:
acc += bet
print[f'Your hand is {hand} with value {hand_val}.\nDealer cards are{dealer_hand} with value {dealer_val}.']
print[f"You win! Your money now is ${acc}."]
elif hand_val == dealer_val:
print[f'Your hand is {hand} with value {hand_val}.\nDealer cards are{dealer_hand} with value {dealer_val}.']
print["Both hands are same. It's draw."]
return acc
# --- Control if user will stop or continue.
def if_continue[money, cards]:
still_play = input["Do you want to play again? Press y to continue, or any other keys if you want to stop: > "].lower[]
if still_play == "y":
game_21[money, cards, status=True]
else:
end_game[money]
# ---- End game ----
def end_game[money]:
print[f"Game over, your money is ${money}."]
# ---- The body of program ----
# ---- Combining all other functions ----
# ---- Indirect recursion to game_21[] from if_continue[] ----
def game_21[money, cards, status]:
''' Core game program.'''
put_bet = bet[money]
print["Let's begin the game."]
hand, hand_val, dealer_hand, dealer_val = dealing[cards]
print[f"Your hand is {hand} with value {hand_val}.\nDealer's first card is {dealer_hand[0]} with value {cards[dealer_hand[0]]}."]
# Need one more recursive here.
while status:
if hand_val < 21:
draw_more = input["Do you want to draw again? Press y to continue, or any other keys if you want to stand: > "].lower[]
if draw_more =="y":
hand, hand_val = add_card[hand, cards]
print[f"Your hand is {hand} with value {hand_val}.\nDealer's first card is {dealer_hand[0]} with value {cards[dealer_hand[0]]}."]
continue
else:
money = open_hand[hand, hand_val, dealer_hand, dealer_val, money, put_bet, cards]
if_continue[money, cards]
elif hand_val == 21:
money += put_bet
print["Blackjack!"]
print[f"Your money now is ${money}."]
if_continue[money, cards]
else:
money -= put_bet
print[f"Your hand value is over than 21. You lose ${put_bet}. Your money now is ${money}."]
if_continue[money, cards]
status = False
# ---- PROGRAM STARTS HERE ----
game_21[initial, the_card, status = True]
1Một điều hữu ích trong quá trình thử nghiệm của tôi là gọi
acc = open_hand[...]
return acc
28 điều đầu tiên. Cài đặt acc = open_hand[...]
return acc
29 tạo ra một trò chơi ngẫu nhiên, trong khi cài đặt acc = open_hand[...]
return acc
30 [hoặc bất kỳ số nguyên nào] tạo ra hành vi xác định, giống nhau mọi lúc…rất hữu ích để thử nghiệm và bạn có thể thay đổi hạt giống để thử các hành vi khác nhauNhân tiện, bạn có thể chưa đủ nâng cao trong quá trình phát triển của mình, nhưng đây có thể là một ý tưởng hay khi sử dụng một lớp để gói gọn trạng thái của trò chơi, thay vì chuyển tất cả các biến khác nhau xung quanh, điều này phức tạp hơn,
Một lưu ý cuối cùng…về mặt kỹ thuật mà nói, chỉ có quân mười và quân Át ở ván bài đầu tiên là một ván bài xì dách, và quân bài xì dách thực sự trả tiền ở mức 3. 2 thay vì 1. 1 tiền cược. Mặt khác, 21 chỉ là 21 và được xử lý giống như bất kỳ mệnh giá thẻ nào khác. Là một thử thách nhỏ, bạn có thể xử lý điều đó như thế nào trong mã?
Chúc mừng, và có thể tỷ lệ cược sẽ có lợi cho bạn
Nhân tiện, trong trường hợp bạn gặp khó khăn hoặc phần trên không rõ ràng, đây là bản sao mã của bạn chỉ áp dụng các điều chỉnh ở trên. Tôi cũng đã tự do thực hiện một sửa đổi bổ sung nhỏ. thay vì trả về
acc = open_hand[...]
return acc
7 trong mọi đường dẫn mã của acc = open_hand[...]
return acc
4, thay vào đó, tôi chỉ gán kết quả của acc = open_hand[...]
return acc
4 cho acc = open_hand[...]
return acc
7 ở phần đầu tiên và trả về acc = open_hand[...]
return acc
7 ở phần cuối. Điều này tránh được rất nhiều mã trùng lặp và khả năng xảy ra lỗiimport random
# Set SEED to None for a random game, or to an integer for a deterministic one
SEED = None
random.seed[SEED]
# ---- INITIALIZATION ----
# Cards
the_card = {
"2" : 2,
"3" : 3,
"4" : 4,
"5" : 5,
"6" : 6,
"7" : 7,
"8" : 8,
"9" : 9,
"10": 10,
"J" : 10,
"Q" : 10,
"K" : 10,
"A" : 11,
}
# Initial account
initial = int[input["How many dollars you bring to the table? $"]]
# ---- Functions ----
# ---- Betting function ----
def bet[acc]:
''' Betting function '''
while True:
your_bet = int[input["How many you willing to bet? $"]]
if your_bet > acc:
print["Your bet is higher than your account. Please repeat all."]
continue
else:
return your_bet
# ---- Draw cards ----
def dealing[cards]:
''' Dealing card function. Return card lists and their value. '''
print["Game start."]
your_card = [random.choice[list[cards.keys[]]] for i in range[0, 2]]
your_val = sum[cards[i] for i in your_card]
dealer_card = [random.choice[list[cards.keys[]]] for i in range[0, 2]]
dealer_val = sum[cards[i] for i in dealer_card]
return your_card, your_val, dealer_card, dealer_val
# ---- Adding card ----
def add_card[hand, cards]:
'''Draw another card '''
new_card = random.choice[list[cards.keys[]]]
hand.append[new_card]
new_val = sum[cards[i] for i in hand]
return hand, new_val
# ---- Compare hands ----
def open_hand[hand, hand_val, dealer_hand, dealer_val, acc, bet, cards]:
''' Checking both hands' cards value. Return user money amount.'''
if dealer_val 21:
# Will be triggered if
acc += bet
print[f'Your hand is {hand} with value {hand_val}.\nDealer cards are{dealer_hand} with value {dealer_val}.']
print[f"You win! Your money now is ${acc}."]
elif hand_val < 21 and dealer_val < 21:
# Triggered when both hands are under 21.
# Whoever have higher hand, wins.
if hand_val < dealer_val:
acc -= bet
print[f'Your hand is {hand} with value {hand_val}.\nDealer cards are{dealer_hand} with value {dealer_val}.']
print[f"You lose. Your money now is ${acc}."]
elif hand_val > dealer_val:
acc += bet
print[f'Your hand is {hand} with value {hand_val}.\nDealer cards are{dealer_hand} with value {dealer_val}.']
print[f"You win! Your money now is ${acc}."]
elif hand_val == dealer_val:
print[f'Your hand is {hand} with value {hand_val}.\nDealer cards are{dealer_hand} with value {dealer_val}.']
print["Both hands are same. It's draw."]
return acc
# --- Control if user will stop or continue.
def if_continue[money, cards]:
still_play = input["Do you want to play again? Press y to continue, or any other keys if you want to stop: > "].lower[]
if still_play == "y":
game_21[money, cards, status=True]
else:
end_game[money]
# ---- End game ----
def end_game[money]:
print[f"Game over, your money is ${money}."]
# ---- The body of program ----
# ---- Combining all other functions ----
# ---- Indirect recursion to game_21[] from if_continue[] ----
def game_21[money, cards, status]:
''' Core game program.'''
put_bet = bet[money]
print["Let's begin the game."]
hand, hand_val, dealer_hand, dealer_val = dealing[cards]
print[f"Your hand is {hand} with value {hand_val}.\nDealer's first card is {dealer_hand[0]} with value {cards[dealer_hand[0]]}."]
# Need one more recursive here.
while status:
if hand_val < 21:
draw_more = input["Do you want to draw again? Press y to continue, or any other keys if you want to stand: > "].lower[]
if draw_more =="y":
hand, hand_val = add_card[hand, cards]
print[f"Your hand is {hand} with value {hand_val}.\nDealer's first card is {dealer_hand[0]} with value {cards[dealer_hand[0]]}."]
continue
else:
money = open_hand[hand, hand_val, dealer_hand, dealer_val, money, put_bet, cards]
if_continue[money, cards]
elif hand_val == 21:
money += put_bet
print["Blackjack!"]
print[f"Your money now is ${money}."]
if_continue[money, cards]
else:
money -= put_bet
print[f"Your hand value is over than 21. You lose ${put_bet}. Your money now is ${money}."]
if_continue[money, cards]
status = False
# ---- PROGRAM STARTS HERE ----
game_21[initial, the_card, status = True]
Đã một năm kể từ khi tôi đặt câu hỏi này và tôi cảm thấy mã của mình trông lộn xộn
Tôi vô tình quay lại câu hỏi này sau một năm và đọc tin nhắn của bạn. Cảm ơn bạn rất nhiều để trả lời câu hỏi khá ngớ ngẩn này. Một năm học python dạy tôi tránh xa đệ quy, và tốt hơn hết là đặt các trách nhiệm khác nhau cho một tệp khác thay vì dồn chúng vào một tệp.
Ở mức độ hiểu biết của tôi bây giờ, tôi có thể tạo một tệp cụ thể cho một lớp có tên Blackjack và đặt tất cả các phương thức của trò chơi vào trong đó. Và có thể một hoặc hai lớp khác để tạo đối tượng người chơi và người chia bài với phương pháp đếm tiền và bàn tay của họ. Tôi cũng sẽ tạo tệp CSV để đặt thẻ và giá trị của chúng hoặc tạo tệp python khác để đặt từ điển riêng. Và sau đó là một tệp python khác làm tệp chính để kết hợp tất cả các chức năng lại với nhau thành một chương trình phù hợp
chỉnh sửa mã của bạn là tuyệt vời. Tôi cần vài tháng để đọc một đoạn mã có thể đọc được như thế
Hiện tại, tôi quan tâm nhiều hơn đến kỹ thuật dữ liệu, quét web và chơi với CSV và JSON. Tôi học được rất nhiều từ ngày tôi viết mã này, đặt câu hỏi [nhưng bỏ qua câu hỏi trong một năm
Ở mức độ hiểu biết của tôi bây giờ, tôi có thể tạo một tệp cụ thể cho một lớp có tên Blackjack và đặt tất cả các phương thức của trò chơi vào trong đó. Và có thể một hoặc hai lớp khác để tạo đối tượng người chơi và người chia bài với phương pháp đếm tiền và bàn tay của họ. Tôi cũng sẽ tạo tệp CSV để đặt thẻ và giá trị của chúng hoặc tạo tệp python khác để đặt từ điển riêng. Và sau đó là một tệp python khác làm tệp chính để kết hợp tất cả các chức năng lại với nhau thành một chương trình phù hợp
Tuyệt, có vẻ như bạn đã học được rất nhiều bài học hay trong năm đó, không chỉ về cú pháp và ngữ nghĩa mà còn về một thứ quan trọng hơn và khó học hơn—thiết kế. Đó là một kiến trúc tốt hơn nhiều, và là một kiến trúc tổng thể khá chắc chắn, và điều đặc biệt thú vị là bạn đã tự mình tìm ra điều đó. Chắc chắn là nhiều tiến bộ hơn tôi đã đạt được trong năm đầu tiên lập trình. Nếu bạn cứ tiếp tục như vậy, tương lai của bạn chắc chắn sẽ tươi sáng. Những lời chúc tốt đẹp nhất trên hành trình lập trình của bạn…và có thể tỷ lệ cược sẽ luôn có lợi cho bạn. Chúc mừng