簡體   English   中英

伙計們,我是 python 新手,請幫助我理解為什么我的代碼給我“無”作為輸出?

[英]Guys I am new to python, can please help me understand why my code is giving me “none” as output?

def deal_card():

    cards = [11, 2, 3, 4, 5, 6, 7, 8, 9, 10, 10, 10, 10]
    card = random.choice(cards)
    return card

user_cards = []

computer_cards = [] 

for _ in range(2):

    user_cards.append(deal_card())
    computer_cards.append(deal_card())

draw_another_card = input("Type 'y' to get another card, type 'n' to pass : ")

if draw_another_card == "y":

    new_user_cards = user_cards.append(deal_card())
    new_computer_cards = computer_cards.append(deal_card())
    print(new_user_cards)

當我打印 new_user_cards 時,它顯示沒有。 我不明白為什么,我用谷歌搜索了很多,但沒有得到任何答案。

隨機函數來自replit。

我真的很想知道上面的代碼出了什么問題!

除了@ThierryLathuille 和@Sujay 的評論。

如果您想要new_user_cardnew_computer_card值,您可以:

代替:

    new_user_cards = user_cards.append(deal_card())
    new_computer_cards = computer_cards.append(deal_card())

經過:

    new_user_card = deal_card()  # without plural because there is only one
    user_cards.append(new_user_card)
    new_computer_card = deal_card()  # without plural because there is only one
    computer_cards.append(new_computer_card)

要么:

    user_cards.append(deal_card())
    new_user_card = user_cards[-1]  # last item of the list
    computer_cards.append(deal_card())
    new_computer_card = computer_cards[-1]  # last item of the list

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM