Python 为什么它不返回任何值?
我想做一个21点游戏 一切正常,直到if和elif声明 如果您在代码平台中尝试此代码,您将看到在If块中“new_user_card”是none type 为什么呢 elif块也是如此 提前谢谢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
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)