回溯(最近通话最近一次):
“ / Users / sam / Downloads / Carsy045(10).py”第9行,位于
从playing_cards导入*
ModuleNotFoundError:没有名为“ playing_cards”的模块
为什么在我尝试运行代码时弹出此消息?!
从playing_cards导入* def get_hit_choice():
“”“
询问用户是击打还是站立。确保
用户输入有效的选择
“”“
选择=输入(“请输入h或s(h =击中,s =站立):”)
同时选择!=“ h”和选择!=“ s”:
choice = input("Please enter h or s (h = Hit, s = Stand): ")
返回选择
def get_play_choice(提示文字):
“”“
显示提示,并强制用户选择是或否
“”“
选择=输入(提示文本)
同时选择!=“ y”和选择!=“ n”:
choice = input(prompt_text)
返回选择
def get_hand_total(hand):
“”“
计算手的分数
“”“
总计= 0
num_aces = 0
#非王牌总数
手中的卡:
if card[0] == "A":
# Separate aces
num_aces += 1
elif card[0] in ["T", "J", "Q", "K"]:
total += 10
else:
total += int(card[0])
#放入A使其不会破裂
#个A是1或11
对于我(范围(num_aces)):
if total + 11 < 21:
total += 11
else:
total += 1
总回报
def card_to_string(card):
“”“
返回卡片的字符串表示形式
“”“
card_str =的“ [p]的card [0] +”
如果卡片[1] ==“ S”:
card_str += "Spades"
elif卡[1] ==“ H”:
card_str += "Hearts"
elif卡[1] ==“ D”:
card_str += "Diamonds"
elif卡[1] ==“ C”:
card_str += "Clubs"
返回card_str
def display_hand(手文本,手):
“”“
显示控制台的手势列表
“”“
#打印标签
print(hand_text +“” + str(get_hand_total(hand))+“:”,end =“”)
#打印卡片
对于范围内的我(len(手)):
print(card_to_string(hand[i]), end="")
if i + 1 < len(hand):
print(" | ", end="")
print()
def play_player_hand(player_hand):
“”“
演奏玩家的手直到其破灭
或者用户选择站立
“”“
stand = False
#继续前进,直到用户选择站立或被摔倒
同时get_hand_total(player_hand)<21且不站立:
choice = get_hit_choice()
print()
if choice == "s":
# Stand is only applicable if the total sum is 15 or greater
if get_hand_total(player_hand) < 15:
print("Cannot stand on value less than 15!")
else:
stand = True
elif choice == "h":
# Add a new card
player_hand.append(deal_one_card())
display_hand("Player's hand is", player_hand)
print()
#返回玩家手牌的总得分
返回get_hand_total(玩家手)
def play_dealer_hand(dealer_hand):
“”“
演奏发牌者的手直到其破灭或到达
最低要求分数
“”“
display_hand(“交易者的手是”,dealer_hand)
同时get_hand_total(dealer_hand)<17:
dealer_hand.append(deal_one_card())
display_hand("Dealer's hand is", dealer_hand)
#返回庄家之手的总得分
返回get_hand_total(经销商手)
def is_black_jack(手):
“”“
检查手是否为黑色插孔
“”“
返回len(hand)== 2和get_hand_total(hand)== 21
def get_card_value(card):
“”“
返回卡片的数值
“”“
如果[[J],“ Q”,“ K”,“ T”]中的卡片[0]:
value = 10
elif卡[0] ==“ A”:
value = 1
其他:
value = int(card[0])
返回值
def play():
“”“
播放新一轮,返回状态是否为
用户赢了,输了或赢得了
“”“
print(“ -------------------- START GAME --------------------”)
#发给发牌手和玩家2张牌
dealer_hand = [deal_one_card(),deal_one_card()]
player_hand = [deal_one_card(),deal_one_card()]
#仅显示庄家的第一张卡
print(“经销商的牌是” + str(get_card_value(dealer_hand [0]))+“:” + card_to_string(dealer_hand [0]))
display_hand(“玩家的手是”,player_hand)
print()
result =“”
#对二十一点做一些初步评估
如果is_black_jack(经销商手)和is_black_jack(玩家手):
result = "draw"
print("*** Blackjack --")
display_hand("Dealer's hand is", dealer_hand)
display_hand("Player's hand is", player_hand)
print()
print("*** Blackjack! Push - no winners! ***")
elif is_black_jack(dealer_hand):
result = "dealer"
display_hand("Dealer's hand is", dealer_hand)
print()
print("*** Blackjack! Dealer Wins! ***")
elif is_black_jack(player_hand):
result = "player"
display_hand("Player's hand is", player_hand)
print()
print("*** Blackjack Player Wins! ***")
其他:
# If nobody got blackjack, time to play each player
player_score = play_player_hand(player_hand)
if player_score > 21:
print("--> Player busts!")
print()
dealer_score = play_dealer_hand(dealer_hand)
if dealer_score > 21:
print()
print("--> Dealer busts!")
# Present the final score
print()
print("--- Dealer: " + str(dealer_score) + " Player: " + str(player_score) + " -> ", end="")
# Evaluate final winner
if (player_score > 21 and dealer_score > 21) or (player_score == dealer_score):
result = "draw"
print("Push - no winners!", end="")
elif player_score > dealer_score:
result = "player"
print("Player Wins!", end="")
else:
result = "dealer"
print("Dealer Wins!", end="")
print(" ---")
print(“ --------------------- END GAME --------------------- “)
返回结果
def main():
“”“
组织游戏和记数程序的入口点
结果
“”“
play_again = get_play_choice(“您想玩BlackJack [y | n]吗?”)
print()
#个用于跟踪统计信息的变量
获胜= 0
丢失= 0
绘制= 0
#只要用户愿意,就继续播放
while play_again ==“ y”:
result = play()
print()
# Tally the result
if result == "player":
won += 1
elif result == "dealer":
lost += 1
else:
drawn += 1
# Ask user if they want to play again
print("That was fun!")
print()
play_again = get_play_choice("Play again [y|n]? ")
print()
#显示统计信息
print(“您玩过” + str(获胜+输掉+抽签)+“游戏!”)
print(“-> Won:” + str(won))
print(“->丢失:” + str(丢失))
print(“->绘制:” + str(绘制))
print()
print(“感谢您的参与!:)”)
main()
答案 0 :(得分:0)
出现此消息是因为您告诉编译器导入playing_cards
模块,但编译器找不到该模块。该import
语句位于脚本的开头。以下所有繁琐的代码都不相关。
要停止获取此消息,您需要在编译器查找模块的目录之一中放置一个Python文件playing_cards.py
。
查看how to write a module和where the compiler looks for modules上的参考文献可能会有所帮助。