假设我需要一个3位数字,所以它会是这样的:
>>> random(3)
563
or
>>> random(5)
26748
>> random(2)
56
Run Code Online (Sandbox Code Playgroud) 我正在使用 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)