V2EX = way to explore
V2EX 是一个关于分享和探索的地方
现在注册
已注册用户请  登录
推荐学习书目
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 · 2020-01-12 21:19:41 +08:00 · 2468 次点击
    这是一个创建于 1564 天前的主题,其中的信息可能已经有所发展或是发生改变。

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

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

    3 条回复    2020-01-13 09:08:49 +08:00
    ericls
        1
    ericls  
       2020-01-12 21:45:21 +08:00 via iPhone
    估计得 frame hack
    lybcyd
        2
    lybcyd  
       2020-01-12 21:48:45 +08:00 via Android
    没太懂这个需求是啥意思。如果单纯为了打印的话,这些不都是函数吗,使用的时候把变量传进去不就行了吗?先定义 start=1,stop=7,然后 range(start, stop)。打印直接打印 start,stop,stop-start
    chenstack
        3
    chenstack  
       2020-01-13 09:08:49 +08:00
    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)
    关于   ·   帮助文档   ·   博客   ·   API   ·   FAQ   ·   我们的愿景   ·   实用小工具   ·   5786 人在线   最高记录 6543   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 285ms · UTC 06:26 · PVG 14:26 · LAX 23:26 · JFK 02:26
    Developed with CodeLauncher
    ♥ Do have faith in what you're doing.