import random #导入random模块
rand = random.randrange(1,100) #设置一个随机数,范围为1-100
#进入循环
while True:
num = int(input('Input a number:'))
if num > rand:
print('The number is too large :<, please try again')
elif num < rand:
print('The number is too small :<, please try again')
else:
print('You are right :)')
break #‘break’的缩进要与print保持一致
进阶版random模块会随机生成一个数字,使用者可以对模块生成的数字进行猜测,如果想要无限机会,可以运用while循环语句,直到猜对的时候使用‘break’来终止程序。
from tkinter import Tk, Label
from tkinter import Button
import random
import time
x = int(input('输入次数:'))
y = int(input('输入范围:1-'))
rand = random.randrange(1, y)
c = b'xe6x88x91xe7x88xb1xe8xb5x96xe8x8bxb1xe5x8dx8exefxbcx81'
nt = time.strftime("%Y-%m-%d %H:%M:%S", time.localtime())
st = time.time()
chance = 0
d = 0b011111100101
f = 0b011111010100
answer = f' 33[0;31;20m{rand} 33[0m'
print('游戏开始时间:', nt)
print(f'游戏现在开始,玩法为猜数字,系统会随机生成一个范围为' 33[0;31;20m1-{y} 33[0m'数字,玩家有 33[0;31;20m{x} 33[0m次机会')
a = b'xe6x81xadxe5x96x9cxe4xbdxa0xe8xa7xa6xe5x8fx91xe9x9ax90xe8x97x8fxe5xbdxa9xe8x9bx8b'
b = b'xe7xadx94xe6xa1x88xe6x98xaf'
for chances in range(1, x + 1):
chance += 1
num = int(input('请输入数字:'))
e = 0b000110100010
g = 0b010001011101
if rand == num:
et = time.time()
ct = int(et - st)
print(f'恭喜你!猜对了 33[0;34;20m:) 33[0m,共用时 33[0;31;20m{ct} 33[0m秒')
break
if chances == x:
print('很遗憾,次数已用完,游戏结束,答案是:' + answer)
break
if y >= num >= rand:
print(f'数字大了,请再试一次,你还有 33[0;31;20m{x - chance} 33[0m次机会')
if num < rand:
print(f'数字小了,请再试一次,你还有 33[0;31;20m{x - chance} 33[0m次机会')
if 1000000 > num > y:
print('超出范围')
if num == int(str(d) + str(e)):
password = int(input('输入代号:'))
if password == int(str(f) + str(g)):
print(a.decode() + ',' + b.decode() + f'{answer}' + ',' + c.decode())
else:
print(f'无效:(,你还有 33[0;31;20m{x - chance} 33[0m次机会')
进阶版增加了现实系统当前时间,玩家可自定义游戏的次数和游玩的范围,猜的数字错误以后会提示还有多少次机会,猜对了则显示猜对数字所需要的时间



