Python初學者小游戲:猜數字
游戲邏輯:電腦隨機生成一個數字,然后玩家猜數字,電腦提示猜的數字大了還是小了,供玩家縮小數字范圍,達到既定次數后,玩家失敗。若在次數內猜對,玩家獲勝。
涉及知識點:random.randint() , print() , input() ( raw_input() )
參考實現代碼:
#!/usr/bin/env python
# encoding: utf-8
#使用print("",end=...)標準
from __future__ import print_function
import os
import sys
import time
import random
#輸入檢測
while 1:
os.system('cls')
print ("Hello , Welcome to Guess_Number Games...The Number is between 1 - 10...")
print ("Please input the level you want(1~10): ",end = '')
level = raw_input("")
diff = 11-int(level)
if diff > 10 or diff <1:
print ("Invalid Input...")
time.sleep(0.3)
else:
break
#猜數字流程
count_num = 0
ran = random.randint(1,10)
while count_num < diff:
count_num += 1
print (str(count_num)+": "+"Please input the number you guess: ",end = '')
number = raw_input()
number = int(number)
if number < ran:
print ("Too Little...")
continue
elif number > ran:
print ("Too Big...")
continue
else:
print ("Congraduation! You Win...")
break
if count_num == diff:
print ("You Lose...")
更多關于python游戲的精彩文章請點擊查看以下專題:
python俄羅斯方塊游戲集合
python經典小游戲匯總
python微信跳一跳游戲集合
以上就是本文的全部內容,希望對大家的學習有所幫助,也希望大家多多支持億速云。
免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。