推荐学习书目
› Learn Python the Hard Way
Python Sites
› PyPI - Python Package Index
› http://diveintopython.org/toc/index.html
› Pocoo
值得关注的项目
› PyPy
› Celery
› Jinja2
› Read the Docs
› gevent
› pyenv
› virtualenv
› Stackless Python
› Beautiful Soup
› 结巴中文分词
› Green Unicorn
› Sentry
› Shovel
› Pyflakes
› pytest
Python 编程
› pep8 Checker
Styles
› PEP 8
› Google Python Style Guide
› Code Style from The Hitchhiker's Guide
huage
V2EX  ›  Python

Python 如何获得 random.randint(1,20)起止数和 range(1,7)起止数及个数

  •  
  •   huage · Jan 12, 2020 · 3315 views
    This topic created in 2450 days ago, the information mentioned may be changed or developed.

    random.randint(1,20),如何获得 1 和 20 这个两个值,用两个变量表示。 range(1,7),如何获得 1 和 7 这个两个值,以及总共有多少个数量,用三个变量表示。

    需要使用这些数值,打印出来。

    3 replies  •  2020-01-13 09:08:49 +08:00
    ericls
        1
    ericls  
       Jan 12, 2020 via iPhone
    估计得 frame hack
    lybcyd
        2
    lybcyd  
       Jan 12, 2020 via Android
    没太懂这个需求是啥意思。如果单纯为了打印的话,这些不都是函数吗,使用的时候把变量传进去不就行了吗?先定义 start=1,stop=7,然后 range(start, stop)。打印直接打印 start,stop,stop-start
    chenstack
        3
    chenstack  
       Jan 13, 2020
    python3 里面 range 是可以获取这些信息的
    r = range(1, 7)
    print(r.start)
    print(r.stop)
    print(len(r))

    random.randint 直接就返回 int 结果了,所以并不能获取,虽然可以 hook 一下,但并没有必要,而且并不适用多线程和多进程
    import random
    import types


    random._randint = random.randint


    random_data = types.SimpleNamespace()


    def randint(a, b):
        random_data.a = a
        random_data.b = b
        return random._randint(a, b)


    random.randint = randint


    rd = random.randint(1, 20)
    print(random_data.a)
    print(random_data.b)
    About   ·   Help   ·   Advertise   ·   Blog   ·   API   ·   FAQ   ·   Privacy   ·   Solana   ·   800 Online   Highest 6679   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 51ms · UTC 21:28 · PVG 05:28 · LAX 14:28 · JFK 17:28
    ♥ Do have faith in what you're doing.