python也可以用來寫個小游戲刽宪,自己玩玩厘贼,在這里練習了一個小游戲——猜數(shù)字界酒。
在聚餐時圣拄,大家可能會玩猜數(shù)字游戲,規(guī)則是這樣的:
指定一個猜數(shù)的范圍毁欣,一個人在這個范圍內(nèi)出一個數(shù)庇谆,然后其他人依次地猜,猜的過程中區(qū)間不斷縮小凭疮,直到有人猜中為止饭耳。
import random
bot=input('Set range bottom\n')
top=input('Set range top\n')
rand=random.randint(bot,top)
print ('Random number in ['+str(bot)+','+str(top)+'] generated!')
num=int(input('###Guess the number###\n'))
cnt=1
while (num!=rand):
if (num<rand):
print('*_* Lower than the answer')
else:
print('T_T Higher than the answer')
num=int(input('###Guess the number###\n'))
cnt=cnt+1
print('^_^ You get the answer with [%d] times'%cnt)