繁体   English   中英

Python:如何修复二十一点程序中的“无法访问的代码”?

[英]Python: How can I fix 'Unreachable code' in my blackjack program?

我在代码的第 14 行收到“代码无法访问”错误。 此外,如果有人对我当前的代码有任何其他提示,我将不胜感激,因为我对这种 python 编程不熟悉。

进口:

import random
from MainProjects.Blackjack.art import logo

print(logo)


# random card selector:
def deal_cards(user_cards, dealer_cards):
    card_list = [11, 2, 3, 4, 5, 6, 7, 8, 9, 10, 10, 10, 10]
    random_card = random.choice(card_list)
    return random_card

    for _ in range(2):
        user_new_card = deal_cards(user_cards=card_list)
        user_cards.append(user_new_card)
    for _ in range(1):
        dealer_new_card = deal_cards(dealer_cards=[])
        dealer_card.append(dealer_new_card)
    print(f"The dealer got the card: {dealer_card}")
    print(f"You got the cards: {user_cards}")

    calculating_scores(card_list)


def calculating_scores(card_list):
    if sum(card_list) == 21 and len(card_list == 2):
        return 0
    if 11 in card_list and sum(card_list) > 21:
        card_list.remove(11)
        card_list.append(1)
    return sum(card_list)


deal_cards(user_cards=[], dealer_cards=[])

start = input("Type 'y' if you'd like to start:\n").lower()

if start == "y" or start == "yes":
    deal_cards(user_cards=[], dealer_cards=[])
else:
    print("Okay, thank you!")
    quit()

您从第 12 行的 function 返回:

return random_card

所以这段代码永远不会执行

    for _ in range(2):
        user_new_card = deal_cards(user_cards=card_list)
        user_cards.append(user_new_card)
    for _ in range(1):
        dealer_new_card = deal_cards(dealer_cards=[])
        dealer_card.append(dealer_new_card)
    print(f"The dealer got the card: {dealer_card}")
    print(f"You got the cards: {user_cards}")

    calculating_scores(card_list)

因为 function 在这一行终止执行并返回给调用者。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM