Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/327.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/python-3.x/19.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 为什么它不返回任何值?_Python_Python 3.x_Blackjack - Fatal编程技术网

Python 为什么它不返回任何值?

Python 为什么它不返回任何值?,python,python-3.x,blackjack,Python,Python 3.x,Blackjack,我想做一个21点游戏 一切正常,直到if和elif声明 如果您在代码平台中尝试此代码,您将看到在If块中“new_user_card”是none type 为什么呢 elif块也是如此 提前谢谢 import random cards = [11, 2, 3, 4, 5, 6, 7, 8, 9, 10, 10, 10, 10] def hand(): user_cards = random.sample(cards, 2) print(f"Your car

我想做一个21点游戏

一切正常,直到if和elif声明

如果您在代码平台中尝试此代码,您将看到在If块中“new_user_card”是none type

为什么呢

elif块也是如此

提前谢谢

    import random
cards = [11, 2, 3, 4, 5, 6, 7, 8, 9, 10, 10, 10, 10]
 
def hand():
    user_cards = random.sample(cards, 2) 
    print(f"Your cards are : \n {user_cards}")
    computer_cards = random.sample(cards,1)
    print(f"Computer's first card {computer_cards}")
    more = input("Do you want to draw another card? Type 'y' for yes and 'n' for no : ")
    if more == "y":
        new_user_card = print(random.choice(cards))
        user_cards.append(new_user_card)
        print(f"Your cards are : \n {user_cards}")
    elif more == "n":
        computer_new_card = print(random.choice(cards,1))
        computer_cards.append(computer_new_card)
        print(f"Computer's has been dealt the second card.\n Computer's cards are {computer_cards}")
    return user_cards
    return computer_cards
    
hand()
print()
函数返回
None
,这里您已将
print(random.choice(cards))
的返回值指定给变量

使用
新用户卡=随机选择(卡)

print(random.choice(cards))
在单独的行上打印(如果需要)。

您使用的是
print
函数,该函数打印到标准输出,但不返回任何值

只要去掉这个函数

例如:

# this is wrong 
# new_user_card = print(random.choice(cards))
new_user_card = random.choice(cards)