简体   繁体   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)

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_cardnew_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.

相关问题 我是python的新手,帮我理解这一点,一行代码? - I am new to python, help me understand this, one line of a code? 请帮我理解这个 python 递归代码? - please help me to understand this code of python recursion? show() function 给了我 15 我明白但是为什么 print(x) function 返回 13 我不明白我是 Z23EEEB4347BDD26BFC6B7EE9A3B755DDs 的新手 - the show() function gives me 15 & I understand but why does the print(x) function return 13 I don't get it I am new to python can you pls help me 请帮助我优化我的Python代码 - Please help me optimize my Python code 您好,我是python新手,在将文件作为输入时遇到困难。 有人可以帮我吗? - Hello, I am new to python and I am having difficulties on having a file as an input. Can somebody help me please? 我的Python正则表达式代码未按预期提供输出 - My Python regex code is not giving me the output as i expected 嘿,我是 python 新手,我在这里遇到问题,你们能帮帮我吗! (文件存在) - Hey all I'm new to python and i'm having a problem here can you guys help me out! (the file exist) 我的 python 代码出现错误,你能帮我修复它吗? - i am getting an error in my python code can you help me fix it? 不明白为什么我的代码给了我一个 IndexError - Don't understand why my code giving me an IndexError 为什么我的python代码不执行,而是给我一个“无”错误,而只执行其中的一部分? - Why is my python code not executing, but instead giving me a “None” error and only executing part of it?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM