Hacktoberfest 2026:维护者为十月标记出来的 issue,仍然开放、适合新手。 浏览 Hacktoberfest issue

other/password.py: alternative_password_generator ignores the requested length when the required characters exceed it

未关闭 适合新手
#15,633 1 条评论 0 个 reaction 已指派 0 人 在 GitHub 查看

维护者通常 2 天内回复

@raaj-2222 已经在做这个了。

开始于 2026年10月25日。

  • #15639 来自 @raaj-2222 —— 未关闭
  • #15645 来自 @saiyam-jain-dev —— 未关闭

评估

难度
2/5
预计耗时
1-3 小时
新手友好度
85/100
Issue 类型
缺陷
描述清晰度
描述清楚
活跃度
活跃
技术栈
python
领域
cli

调研方向

打开 other/password.py 并阅读 alternative_password_generator;bug 位于 i -= len(chars_incl) / quotient = i // 3 / remainder = i % 3 这一代码块中,当 i 变为负数时行为异常。用 issue 中的代码片段复现问题,然后决定当 len(chars_incl) > i 时是抛出 ValueError 还是进行钳制,并检查 main() 是否需要报告该错误。完成的含义是两个复现场景都返回所请求的长度,且对有效输入的现有行为保持不变。

由索引模型根据 Issue 内容生成。

描述

Repository commit

6e03a48579ca212a08b36be00378f887dc41a003

Python version (python --version)

Python 3.13.5

Dependencies version (pip freeze)

None needed — other/password.py only uses the standard library (secrets, random, string).

Expected behavior

alternative_password_generator(chars_incl, i) should return a password whose length matches the requested length i (the main() prompt calls it "the max length of your password", and the comment above the function says i = how many letters or characters the password length will be).

Actual behavior

When chars_incl is longer than the requested length i, the subtraction i -= len(chars_incl) makes i negative, and the generator silently returns a password longer than what was asked for:

>>> from other.password import alternative_password_generator
>>> len(alternative_password_generator("abcdefgh", 5))   # asked for 5
8
>>> len(alternative_password_generator("abcd", 3))      # asked for 3
5

In the second case i becomes -1, so quotient = i // 3 is -1 and remainder = i % 3 is 2 (Python's modulo of a negative number is positive), which adds one extra random letter on top of all the required characters.

A user who types a max length smaller than their required-characters string in main() gets a password that silently exceeds the max length they asked for. If the requested length is at least len(chars_incl) everything works as expected.

Steps to reproduce

python - <<'EOF'
from other.password import alternative_password_generator
print(len(alternative_password_generator("abcdefgh", 5)))  # expected 5, prints 8
print(len(alternative_password_generator("abcd", 3)))      # expected 3, prints 5
EOF

Suspected cause

The negative remainder path in alternative_password_generator:

i -= len(chars_incl)
quotient = i // 3
remainder = i % 3

When len(chars_incl) > i, quotient goes negative (the random() calls then contribute nothing because range() of a negative is empty) while remainder stays positive, so quotient + remainder adds extra characters. Guarding the input (if i < len(chars_incl): raise ValueError(...) or clamping) would keep the generator honest about the requested length.

主要语言
Python
星标
225k
派生
51.2k
平均合并
1 天 13 小时
30 天内合并 PR
32

环境准备

在 Codespaces 中打开

在浏览器里用你自己的 GitHub 账号启动这个项目的开发容器。

从这里开始

  1. 先读完整个 Issue,再读项目的贡献指南。
  2. 在 Issue 下留言说明你要接手 —— 这能避免两个人做同样的事。
  3. Fork 仓库,在一个分支上完成修改。
  4. 提交 Pull Request,并在描述里引用这个 Issue 编号。

TheAlgorithms/Python 的其他 Issue

查看 TheAlgorithms/Python 的全部 Issue

相似的 Issue

更多 Python Issue

把新 issue 发到你的邮箱

精选适合新手参与的 GitHub issue 摘要。