Python开发Go Fish游戏时函数跳过及ask_comp()变量引用错误问题
Go Fish卡牌游戏错误修复说明
核心问题汇总
- 游戏主逻辑未执行,所有函数被跳过
- 写完
game()函数后没有主动调用,脚本运行时不会自动执行该函数 game()函数内循环条件写错:while is_game_over == False是直接比较函数对象本身,不会触发函数执行判断游戏状态,需要改为调用函数while not is_game_over()
- 写完
draw_card()逻辑顺序错误- 先执行了取卡、return操作,后面的空牌堆判断永远不会被执行
- 空牌堆判断条件错误,
deck == 0要改为len(deck) == 0,且需要把空牌堆判断逻辑放在取卡操作之前
ask_comp()报错“local variable referenced before assignment”及random.choice调用错误- 变量名拼写错误:定义的列表是
potential_cards,循环内写的是potential_card.append(),少了后缀s,且append没有传入要添加的card参数 random.choice是模块级函数,不是列表的内置方法,potential_cards.choice()、comp.choice()写法都错误,要改为choice(potential_cards)、choice(comp)- 当
comp手牌为空时,else分支里取choice(comp)也会报错,需补全边界判断
- 变量名拼写错误:定义的列表是
- 其他逻辑错误
play_again()里重置牌堆逻辑错误:deck.append(cards)是把整个卡牌列表作为单个元素加入牌堆,正确写法是deck[:] = cards * 4score()计分逻辑错误:规则是凑齐4张的书才计分,当前逻辑是统计所有不同卡牌的点数总和,不符合规则- 游戏结束后直接递归调用
game(),没有调用play_again()判断用户意愿,会导致无限递归
修正后的完整代码
from random import randint from random import choice cards = ["2", "3", "4", "5", "6", "7", "8", "9", "10", "J", "Q", "K", "A"] deck = cards * 4 user = [] comp = [] point_value = {"2":2, "3":3, "4":4, "5":5, "6":6, "7":7, "8":8, "9":9, "10":10, "J":11, "Q":12, "K":13, "A":14} def draw_card() -> str: """draws a single card and removes it from deck""" if len(deck) == 0: print("Deck is out of cards") return None card = deck[randint(0, len(deck)-1)] deck.remove(card) return card def deal_cards(): """deals cards for user and computer""" for i in range(7): user.append(draw_card()) comp.append(draw_card()) user.sort() comp.sort() def go_fish(hand:list) -> str: """Prints go fish and draws a card""" print("Go fish!") card = draw_card() if not card: return None hand.append(card) hand.sort() if hand is user: print("Now adding " + card + " to your hand.") else: print("The computer has drawn a card.") return card def give_cards(giver:list, taker:list, card:str) -> tuple: """Passes cards from one hand to another""" print("Transfering card " + card) while card in giver: giver.remove(card) taker.append(card) taker.sort() return giver, taker def display(): """Display the contents of the hands""" print("Your cards are: ", user) print("The computer has ", len(comp), " cards.") def play_again() -> bool: """prompts user if they wish to play again""" while True: resp = input("Would you like to play again? (Y/N): ") resp = resp.upper() if resp == "Y": user.clear() comp.clear() deck[:] = cards *4 return True if resp == "N": return False print("Invalid input") def welcome_message(): """Prints Welcome Message""" print("Welcome to Go Fish!") input("Press Enter to play!") print( "Rules: There are two players: You and the computer. " + \ "Your goal is to make as many 'books' or four of a kind as you can. "+ \ "Book points are based on the card. " + \ "Aces are the highest points and 2s are the lowest. " + \ "Now let's play! Good Luck!") input("Press enter to proceed!") def is_game_over()-> bool: """Checks if deck is empty to determine if game is over""" if len(deck)>= 1: return False for card in set(comp): if comp.count(card) < 4: return False for card in set(user): if user.count(card) < 4: return False return True def ask_comp(): """Asks computer to request a card""" potential_cards = [] for card in set(comp): if comp.count(card) < 4: potential_cards.append(card) if len(potential_cards) > 0: random_card = choice(potential_cards) print("The computer is requesting " + random_card) else: if not comp: return random_card = choice(comp) if random_card in user: print("You have " + random_card + " card and the computer will now take it") give_cards(user, comp, random_card) else: print("You don't have " + random_card + " card and now the computer will Go Fish!") go_fish(comp) def ask_user(): """Asks the user to request a card""" invalid = True while invalid: print("What card would you like? ") resp = input("Enter something from " + str(user) + ": ") resp = resp.upper() if resp in user: invalid = False else: print("Invalid response") if resp in comp: print("Computer has card") give_cards(comp, user, resp) else: print("Computer does not have card") go_fish(user) def score(): """Lets player know the score of the game""" comp_total = 0 user_total = 0 for card_type in set(comp): if comp.count(card_type) ==4: comp_total += point_value[card_type] for card_type in set(user): if user.count(card_type) ==4: user_total += point_value[card_type] if user_total > comp_total: print("You won! The computer lost in Go Fish to you!") elif user_total < comp_total: print("You lost! The computer won Go Fish!") else: print("You tied with the computer in Go Fish. Congrats!") def game(): """starts game""" welcome_message() deal_cards() while not is_game_over(): ask_user() if is_game_over(): break ask_comp() display() print("The game is over. Hope you had fun!") score() if play_again(): game() else: print("Thanks for playing!") # 主动调用启动游戏 if __name__ == "__main__": game()
内容的提问来源于stack exchange,提问作者superflygemini
相关产品推荐
相关产品推荐

