【问题标题】:why does this return none value?为什么这不返回任何值?
【发布时间】:2021-04-30 21:40:31
【问题描述】:

我正在尝试制作二十一点游戏

在 if 和 elif 语句之前一切正常...

如果您在代码平台中尝试此代码,您将在 if 块中看到“new_user_card”是无类型..

这是为什么呢?

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()

【问题讨论】:

    标签: python python-3.x blackjack


    【解决方案1】:

    print()函数返回None,这里你已经将print(random.choice(cards))的返回值赋给了你的变量。

    使用 new_user_card = random.choice(cards) 并添加 print(random.choice(cards)) 在单独的行上,如果需要的话。

    【讨论】:

      【解决方案2】:

      您正在使用print 函数,该函数打印到标准输出,但返回无。

      只要去掉这个函数。

      例如:

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

      【讨论】:

        猜你喜欢
        • 2020-09-28
        • 1970-01-01
        • 2018-01-24
        • 1970-01-01
        • 2015-11-03
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多