相关疑难解决方法(0)

如何在python中生成具有特定长度的随机数

假设我需要一个3位数字,所以它会是这样的:

>>> random(3)
563

or

>>> random(5)
26748
>> random(2)
56
Run Code Online (Sandbox Code Playgroud)

python random

89
推荐指数
6
解决办法
12万
查看次数

如何创建一个随机的 4 位数字并将其存储为变量

我正在使用 python 2.7.10 进行猜 4 位数字游戏。但是我找不到如何随机生成一个 4 位数字并将其存储为变量。数字必须介于 1111 和 9999 之间。困难的部分是,我想将其存储为变量,而不是将其打印出来供玩家查看。

如果你愿意,我会在完成后发布代码。

代码终于来了!

import random
number = random.randint(1111,9999)
tryagain = 'yes'
print ('hello! ')
print ('------------------------------------------------------------- ')
print ('Try and guess the 4 digit number in the least number of tries ')
print ('------------------------------------------------------------- ')
while tryagain == 'yes':
    lowoutput = None
    highoutput = None
    guess = None
    guess = input('Guess: ')
    if guess == number:
        print ('Correct! ')
        correctoutput str(raw_input(Enter 'exit' to exit: "))
        If correctoutput == 'exit': …
Run Code Online (Sandbox Code Playgroud)

python windows random

0
推荐指数
1
解决办法
8663
查看次数

标签 统计

python ×2

random ×2

windows ×1