Python双人猜数字投注游戏单局答案重置Bug排查问询
双人数字竞猜投注游戏逻辑异常修复
问题描述
运行双人数字竞猜投注游戏时,若玩家未精准猜中目标数,每轮高低提示存在明显矛盾:
- 同一局内猜34提示太高,猜33又提示太低
- 前两轮提示可推断答案在
65<x<74区间,5次尝试耗尽后公布的正确答案却是3
初步判断为每轮循环都重置了正确数字,调整过(correctnum) = int(random.randint(1, 100))在while循环内的位置始终无法解决,需要明确目标随机数的正确生成位置,保证单局游戏内答案固定。
问题原代码
import random print('Welcome to the Number Guessing Game!') print('The rules of the game are simple. If betting is chosen, please enter your bets.') name1 = input('Please enter name of Player 1: ') name2 = input('Please enter name of Player 2: ') bet = input('Would you like to bet? Please answer with a "Yes" or "No": ') betpool = 0 if bet == 'Yes': bettingamt = float(input('What would your bet be? Please input the number that everyone will equally contribute to, in integer form: ')) if bettingamt > 0: betpool += bettingamt*2 print('The betpool is now currently ${:.2f}'.format(betpool)) else: print('You have not decided to bet, and will not be able to bet until the answer is achieved.') elif bet == 'No': print('You have not decided to bet, and will not be able to bet until the answer is achieved.') tries = 0 while True: (correctnum) = int(random.randint(1, 100)) tries += 1 (num1) = int(input('Try {}: Enter a number between 1 and 100 for Player 1 (or -1 to end, -2 to end and give up): '.format(tries))) (num2) = int(input('Try {}: Enter a number between 1 and 100 for Player 2 (or -1 to end, -2 to end and give up): '.format(tries))) if num1 == -1: print('{} has forfeited. {} wins by default! The betpool of {} is now {}!'.format(name1, name2, name1, name1)) break if num2 == -1: print('{} has forfeited. {} wins by default! The betpool of {} is now {}!'.format(name2, name1, name2, name2)) break if num1 == correctnum: print() print('Congratulations {}! You have won the betpool of {}!'.format(name1, betpool)) print() print('Do come again! Bye-Bye!') print('Better luck next time, {}.'.format(name2)) break elif num2 == correctnum: print() print('Congratulations {}! You have won the betpool of {}!'.format(name2, betpool)) print() print('Do come again! Bye-Bye!') print('Better luck next time, {}.'.format(name1)) break if num1 != correctnum: if num1 > correctnum: print('{} is too high'.format(num1)) elif num1 < correctnum: print('{} is too low'.format(num1)) if num2 != correctnum: if num2 > correctnum: print('{} is too high'.format(num2)) elif num2 < correctnum: print('{} is too low'.format(num2)) if tries == 5: print('Unlucky run, you got it wrong. The correct answer was {}'.format(correctnum)) print() retry = input('Would you like to retry? If yes, your old bets will be added to the pot and you are now required to re-bet the same amount as the previous times. (Yes/No) ') if retry == 'Yes': print('You have chosen to retry. Your old bets will now be added onto the pot.') print('You have decided to play again.') print('Your bet has been renewed to the betpool.') print() betpool += betpool print('The new betpool is now ${}.'.format(betpool)) tries -= 5 print('Your number of tries is now {}'.format(tries)) continue else: print('You have decided to give up, and thus your betpool shall now be nulled. Thank you for playing, we do hope to see you again.') break
异常输出示例
示例1
Try 2: Enter a number between 1 and 100 for Player 1 (or -1 to end, -2 to end and give up): 34 Try 2: Enter a number between 1 and 100 for Player 2 (or -1 to end, -2 to end and give up): 34 34 is too high 34 is too high Try 3: Enter a number between 1 and 100 for Player 1 (or -1 to end, -2 to end and give up): 33 Try 3: Enter a number between 1 and 100 for Player 2 (or -1 to end, -2 to end and give up): 33 33 is too low 33 is too low
示例2
Try 1: Enter a number between 1 and 100 for Player 1 (or -1 to end, -2 to end and give up): 34 Try 1: Enter a number between 1 and 100 for Player 2 (or -1 to end, -2 to end and give up): 65 34 is too low 65 is too low Try 2: Enter a number between 1 and 100 for Player 1 (or -1 to end, -2 to end and give up): 74 Try 2: Enter a number between 1 and 100 for Player 2 (or -1 to end, -2 to end and give up): 88 74 is too high 88 is too high Try 3: Enter a number between 1 and 100 for Player 1 (or -1 to end, -2 to end and give up): 72 Try 3: Enter a number between 1 and 100 for Player 2 (or -1 to end, -2 to end and give up): 69 72 is too high 69 is too high Try 4: Enter a number between 1 and 100 for Player 1 (or -1 to end, -2 to end and give up): 72 Try 4: Enter a number between 1 and 100 for Player 2 (or -1 to end, -2 to end and give up): 69 72 is too high 69 is too high Try 5: Enter a number between 1 and 100 for Player 1 (or -1 to end, -2 to end and give up): 72 Try 5: Enter a number between 1 and 100 for Player 2 (or -1 to end, -2 to end and give up): 69 72 is too high 69 is too high Unlucky run, you got it wrong. The correct answer was 3
根因分析
异常的核心原因是目标随机数生成语句放在了while True主循环内部:每轮循环启动时都会重新生成一个1-100的随机数作为correctnum,每轮给出的高低提示都是对应当轮新生成的答案,和之前轮次的答案完全无关,自然会出现提示前后矛盾、最终公布答案和之前推断区间完全不符的问题。
单纯调整该语句在while循环内的位置无法解决问题,只要语句还在循环体内,每轮都会重新生成随机数。
修复方案
- 将随机数生成语句移出while循环,放在
tries = 0初始化完成后、进入游戏主循环之前,保证单局5次尝试全程使用同一个固定正确答案。 - 在玩家选择重开(retry输入Yes)、重置尝试次数的逻辑分支中,补充一次随机数生成逻辑,给新一局游戏生成新的目标数,避免重开后沿用上一局答案。
- 变量定义外多余的括号不影响逻辑运行,可以直接去掉精简代码。
修复后的关键代码片段:
# 省略前置的欢迎语、玩家信息录入、投注逻辑 tries = 0 # 首次开局生成固定正确答案 correctnum = random.randint(1, 100) while True: tries += 1 # 注意:循环内不要再放置correctnum的生成语句 num1 = int(input('Try {}: Enter a number between 1 and 100 for Player 1 (or -1 to end, -2 to end and give up): '.format(tries))) num2 = int(input('Try {}: Enter a number between 1 and 100 for Player 2 (or -1 to end, -2 to end and give up): '.format(tries))) # 省略弃权判断、猜中判断、高低提示逻辑 if tries == 5: print('Unlucky run, you got it wrong. The correct answer was {}'.format(correctnum)) retry = input('Would you like to retry? If yes, your old bets will be added to the pot and you are now required to re-bet the same amount as the previous times. (Yes/No) ') if retry == 'Yes': # 省略投注累加、提示打印逻辑 betpool += betpool print('The new betpool is now ${}.'.format(betpool)) tries -= 5 print('Your number of tries is now {}'.format(tries)) # 重开新局时生成新的随机答案 correctnum = random.randint(1, 100) continue else: print('You have decided to give up, and thus your betpool shall now be nulled. Thank you for playing, we do hope to see you again.') break
内容的提问来源于stack exchange,提问作者pengs
相关产品推荐
相关产品推荐

