Python猜數字游戲
[Python]代碼
import random
def run():
a=[] #存放4個數字
x=range(10)
for i in range(4):
a+=str(x.pop(random.randrange(10-i))) #從10個數字里選4個
b=set(a) #用于判斷猜中幾個數
end=True #開始猜循環
t=0 #累計猜的次數
while end:
A=0 #數字位置都正確
C=0 #數字猜中數
while True:
c=raw_input('input four number:')
if c in ['i love you','love you','kiss you','miss you']: #作弊
print("Thank you! the answer is {0}".format(a))
continue
elif len(c)!=4:
print('Four number,please!')
continue
elif len(set(c))!=4:
print('Four different number,ok?')
continue
else:
break
for i in range(4):
if c[i]==a[i]:
A+=1
if c[i] in b:
C+=1
t+=1
if A==4:
print("Congratulations!You've just got it after {0} times".format(t))
end=False #結束游戲
else:
print("{0:2d}A{1:2d}B".format(A,C-A)) #給提示。(C-A即猜中但位置不對的數)
if t==10:
print("Are you tired?")
if t==11:
print("If you need help,just say'i love you' ^_^")
run() 本文由用戶 y0657bys 自行上傳分享,僅供網友學習交流。所有權歸原作者,若您的權利被侵害,請聯系管理員。
轉載本站原創文章,請注明出處,并保留原始鏈接、圖片水印。
本站是一個以用戶分享為主的開源技術平臺,歡迎各類分享!