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 檢視

維護者通常 1 天內回覆

@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 天 8 小時
30 天內合併 PR
40

環境準備

在 Codespaces 中開啟

在瀏覽器裡用你自己的 GitHub 帳號啟動這個專案的開發容器。

從這裡開始

  1. 先讀完整個 Issue,再讀專案的貢獻指南。
  2. 在 Issue 下留言說明你要接手 —— 這能避免兩個人做同樣的事。
  3. Fork 儲存庫,在一個分支上完成修改。
  4. 送出 Pull Request,並在描述裡引用這個 Issue 編號。

TheAlgorithms/Python 的其他 Issue

查看 TheAlgorithms/Python 的全部 Issue

相似的 Issue

更多 Python Issue

把新 issue 寄到你的電子郵件信箱

精選適合新手參與的 GitHub issue 摘要。