简体   繁体   中英

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)

when I print new_user_cards, it shows none. I don't understand why, I googled a lot but couldn't get any answers.

The random function is from replit.

I would really like to what is going wrong in the above code!

In addition to the comments of @ThierryLathuille and @Sujay.

If you want the new_user_card and new_computer_card values, you can:

Replace:

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

By:

    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)

Or:

    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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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