代碼如下:
import time, random
class GuessNum:
def __init__(self):
self._num = ''
self.input_num = []
self.count = 1 #猜對所用次數
self.sec = 0 #猜對所用時間
self._generate_num()
def _generate_num(self): #產生不重復的四個數字
seq_zton = list(range(10))
for i in range(0, 4):
a = str(random.choice(seq_zton)) #選出一個數字
self._num += a
seq_zton.remove(int(a)) #注意a的類型
self.sec = time.clock() #開始計時
def check_answer(self):
return self._num
def check_input(self):
num_pos, num_value = 0, 0 #位置對和數值對的分別的個數
tmp = input("Please input the number you guess(No repetition),or 'c' to check the answer:")
if tmp == 'c':
print(self.check_answer())
tof = self.check_input()
return tof
elif not tmp.isalnum or not len(tmp) == 4:
print("Wrong format!")
tof = self.check_input() #需要優化
新聞熱點
疑難解答