如何使用 python 3.2 生成(并标记)随机整数?

发布于 2024-10-08 20:40:27 字数 235 浏览 0 评论 0原文

好吧,我承认我是编程新手,但我无法确定如何让 python v3.2 在我给定的参数之间生成随机正整数。为了让您能够理解上下文,我尝试创建一个猜谜游戏,其中用户输入参数(例如 1 到 50),然后计算机在给定数字之间生成一个随机数。然后用户必须猜测计算机选择的值。我已经进行了漫长而艰苦的搜索,但我能找到的所有解决方案都只告诉一个人如何获取早期版本的 python 来生成随机整数。据我所知,v.3.2 改变了生成和标记随机整数的方式。有人知道该怎么做吗?

Okay, so I'm admittedly a newbie to programming, but I can't determine how to get python v3.2 to generate a random positive integer between parameters I've given it. Just so you can understand the context, I'm trying to create a guessing-game where the user inputs parameters (say 1 to 50), and the computer generates a random number between the given numbers. The user would then have to guess the value that the computer has chosen. I've searched long and hard, but all of the solutions I can find only tell one how to get earlier versions of python to generate a random integer. As near as I can tell, v.3.2 changed how to generate and label a random integer. Anyone know how to do this?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

浪菊怪哟 2024-10-15 20:40:27

使用 random.randrangerandom.randint (请注意,链接指向 Python 3k 文档)。

In [67]: import random
In [69]: random.randrange(1,10)
Out[69]: 8

Use random.randrange or random.randint (Note the links are to the Python 3k docs).

In [67]: import random
In [69]: random.randrange(1,10)
Out[69]: 8
笙痞 2024-10-15 20:40:27

您可以使用random模块:

import random

# Random integer >= 5 and < 10
random.randrange(5, 10)

You can use random module:

import random

# Random integer >= 5 and < 10
random.randrange(5, 10)
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文