python - 猜谜游戏中输入多个相同的数字;循环停止工作?

标签 python list loops random input

我一直在制作一个 4 猜数字游戏来学习 Python,它满足三个标准:

  • 可重播
  • 告诉玩家需要尝试多少次才能猜出正确答案
  • 告诉玩家有多少个数字是正确的,以引导玩家找到正确的答案

我以为我符合标准,但游戏中出现了一个非常奇怪的错误。如果您尝试通过反复试验来猜测数字;游戏中断并且没有检测到您的答案是正确的。如果答案是“[1, 2, 3, 4]”,并且您尝试通过执行“[1, 1, 1, 1]”然后“[1, 2, 2, 2,]”来获得答案,最终获取“[1,2,3,4]”;程序会说这 4 个数字匹配,但不会让你赢得游戏,只会要求你再玩一次。这个错误真的要了我的命,我希望阅读的人能理解我想说的。

抱歉,代码块又大又长,但是问题可能出在此处的任何地方,但老实说我看不到它;我会尽可能地注释,以使其看起来不那么困惑。我只是...为什么会发生这种事!?

def compareLists(a, b): # to compare the guessed numbers and random numbers
    return list(set(a) & set(b))
rNums = random.sample(range(10), 4) # random list of numbers
def start():
    count = 0 # count for number of tries
    global rNums
    gNums = [] # guessed numbers
    print(rNums) # cheating to save time
    flag = True # to stop the guessing loop

    while flag:
        print("Get ready to guess 4 numbers!")
        for i in range(0, 4): # asks the player 4 times to input a number
            x = int(input("Guess: "))
            gNums.append(x) # puts numbers in guessed numbers

        comparison = len(compareLists(rNums, gNums)) # storing the length of the list of similar numbers
        isCorrect = gNums == rNums # to check if lists are the same
        print("You guessed: ", gNums) # telling player what they have guessed

        if isCorrect: # if the lists are the same

            if count > 1:
                print("You win!! It only took you %d tries!" %count) # telling the player how many tries it took
            else: #congratulating the player on a flawless guess
                print("I CAN'T BELIEVE WHAT I'M SEEING!!!")
                print("YOU GOT IT IN ONE GO!!")
            count += 1 # increment count
            rNums = random.sample(range(10), 4) # generate new numbers
            gNums.clear()
            pAgain = input("Play again?")
            if pAgain.lower() in ('y', 'yes'): # replaying the game
                continue
            elif pAgain.lower() in ('n', 'no'):
                flag = False
            else:
                print("Incorrect syntax!")

        else:
            print("You guessed " + str(comparison) + " numbers right, keep guessing!") # tells the player how many numbers are similar so the player can get a more educated guess
            gNums.clear() # empties guessed numbers
            count += 1 # increment count
            print("Number of tries so far: %d" %count) # showing player number of tries so far

最佳答案

用于检查两个列表是否相同的比较不起作用:

isCorrect = gNums == rNums # to check if lists are the same

上面的代码检查两个列表是否相同,但元素的顺序必须相同。

对于您的测试,您只需检查匹配的数字(忽略顺序)是否等于数字列表的长度:

isCorrect = comparison == len(gNums) # to check if lists are the same

有关比较列表(无论顺序如何)的更多信息,请参阅此 answer .

此外,在与 1 进行比较之前,您应该增加计数,否则程序会说您只进行了一次,而实际上您进行了两次。

关于python - 猜谜游戏中输入多个相同的数字;循环停止工作?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33185558/

相关文章:

python - 函数 'cv::resize' 中的 OpenCV(4.1.2) 错误 : (-215:Assertion failed) ! ssize.empty()

python 在 Mac OS X 中更改键盘语言

c# - 如何检查一个对象是否是列表枚举类型的对象?

c++ - 开关盒避免循环

java - 初学者作业需要帮助!扫描文件 -> 数据验证 -> Printwriter

python - 将标准输出重定向到 tkinter 文本小部件

Python:检查/dev/disk 设备是否存在

python - 查找 : any one of the substrings (whichever first) stored in a list; in a bigger string in Python 的出现位置

正则表达式,冒号分隔列表

java - 带用户输入的 For 循环。输出以空格开头