[英]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.当我打印 new_user_cards 时,它显示没有。 I don't understand why, I googled a lot but couldn't get any answers.
我不明白为什么,我用谷歌搜索了很多,但没有得到任何答案。
The random function is from replit.随机函数来自replit。
I would really like to what is going wrong in the above code!我真的很想知道上面的代码出了什么问题!
In addition to the comments of @ThierryLathuille and @Sujay.除了@ThierryLathuille 和@Sujay 的评论。
If you want the new_user_card
and new_computer_card
values, you can:如果您想要
new_user_card
和new_computer_card
值,您可以:
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
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.