You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Python新手开发僵尸末日游戏遇验证码匹配正确仍重复验证问题

问题修复方案

核心错误原因

  • 类型不匹配:你生成的passcode是5位字符串类型,用户输入被你用int()强制转成了整数,不同类型的变量比较结果永远为False,因此输入正确也会提示不匹配。
  • 全局变量作用域错误:start()函数内修改passwordright时没有声明为全局变量,Python会将其识别为函数局部变量,全局的passwordright永远保持False,导致验证环节无限重复。
  • 逻辑冲突:你同时使用了递归调用和while循环处理验证失败场景,两套重试逻辑叠加导致流程完全混乱。

修复后的完整代码

import time
import sys
import random


def typing(text):
    for character in text:
        sys.stdout.write(character)
        sys.stdout.flush()
        time.sleep(0.07)


def fasttyping(text):
    for character in text:
        sys.stdout.write(character)
        sys.stdout.flush()
        time.sleep(0.02)


def fastertyping(text):
    for character in text:
        sys.stdout.write(character)
        sys.stdout.flush()
        time.sleep(0.005)


passcode = "".join(str(random.randint(1, 9)) for _ in range(5))
typing("Hello fellow surviver.")
time.sleep(0.8)
print("")
typing("Welcome to the Zombie Military Base: ")
time.sleep(0.8)
print("")
typing("Lambda 17 ")
time.sleep(1)
print("")
typing("This is where you will begin your survival story. ")
print("")
time.sleep(1)
passwordright = False
typing(f"Type [{passcode}] to verify as a Human")
time.sleep(0.5)
typing(".")
time.sleep(0.3)
typing(".")
time.sleep(0.3)
typing(".")
time.sleep(0.3)


def start():
    global passwordright
    while not passwordright:
        print("")
        typing(">>> ")
        o = input("")
        if o != passcode:
            typing("Please try again.")
            print("")
        else:
            passwordright = True
            fasttyping("#@$____!#%$!@___WE_ARE_COMING_FOR_YOU__@$@#$___@#$")
            time.sleep(0.2)
            sys.stdout.flush()
            time.sleep(1)
            fastertyping(
                '\r____________________________________________________________')
            time.sleep(0.5)
            print("")
            typing("Since you are here, you will need a nickname.")
            print("")
            fasttyping("What will !@#$%^&@")
            time.sleep(0.2)
            sys.stdout.flush()
            time.sleep(1)
            fastertyping('\rWhat will your name be?')


start()

改动说明

  • 移除了输入的int()转换,直接读取用户输入的字符串和passcode比较
  • 函数开头加了global passwordright声明,确保修改的是全局变量
  • 把递归重试改成了函数内的while循环重试,移除了冗余的循环逻辑,避免流程混乱

内容的提问来源于stack exchange,提问作者SomeonenamedPratik

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.09.26 10:06:02