AB猜數字(不行在這沒有互動的終端機上跑)

Run Settings
LanguagePython
Language Version
Run Command
import random import re import functools howManyShouldPick = 0; while True: tmp = re.search( "\d+" , input("要猜幾個數字,也就是說要生成幾個亂數") ); if tmp != None : howManyShouldPick = int(tmp.group()); break; randPick = random.sample( range(0,9), int( howManyShouldPick ) ); countForHowManyTimes = 0; inputString = ""; print("猜數字,有 {} 個數字(每一個範圍是0 .. 9)".format( howManyShouldPick )); while True: while True: tmp = re.search( "^\d{{{}}}$".format( howManyShouldPick ), input("請輸入 {} 個數字,不能多不能少".format(howManyShouldPick)) ); if tmp != None : inputString = list(map(int,tmp.group())); break; print(''' 你答的次數有 {} 次 你答對的元素有 {} 個 你答對的順序有 {} 個 '''.format(++countForHowManyTimes, len( set(inputString) & set(randPick) ), sum(list(map( lambda x, y: x == y , inputString, randPick) )))); if functools.reduce(lambda x, y: x and y ,list(map( lambda x, y: x == y , inputString, randPick))): break; print("你答對了,恭喜");
Editor Settings
Theme
Key bindings
Full width
Lines