|
发表于 2018-4-8 22:33:13
|
显示全部楼层
- import random
- ans = random.sample([1,2,3,4,5,6,7,8,9],4)
- #print(ans)
- i = 1
- scan_i = []
- guess_i = []
- while True :
- guess_0 = input("Please input a Four bit non repeating number,Q - quit the game:")
- if guess_0 == 'Q' :
- print('The answer is '+str(ans))
- print('Quiting...')
- break
- guess_0 = int(guess_0)
- k1 = guess_0//1000
- k2 = guess_0%1000//100
- k3 = guess_0%1000%100//10
- k4 = guess_0%10
- guess = [k1,k2,k3,k4]
- if guess == ans :
- guess_00 = str(ans[0])+str(ans[1])+str(ans[2])+str(ans[3])
- print('You are right!The answer is '+str(guess_0))
- break
- A = 0
- B = 0
- for k in range(4):
- if guess[k] == ans[k]:
- A += 1
- if ((guess[k] in ans) == True) and (guess[k] != ans[k]):
- B += 1
- scan = str(A)+'A'+str(B)+'B'
- guess_i.append(guess_0)
- scan_i.append(scan)
- print('HISTORY:')
- for j in range(i):
- print(str(j+1)+". "+str(guess_i[j])+" --> "+str(scan_i[j]))
- i += 1
复制代码
结果展示。。
- ================= RESTART: /Users/nizen/Documents/每日一题175.py =================
- Please input a Four bit non repeating number,Q - quit the game:1234
- HISTORY:
- 1. 1234 --> 0A2B
- Please input a Four bit non repeating number,Q - quit the game:4321
- HISTORY:
- 1. 1234 --> 0A2B
- 2. 4321 --> 1A1B
- Please input a Four bit non repeating number,Q - quit the game:4325
- HISTORY:
- 1. 1234 --> 0A2B
- 2. 4321 --> 1A1B
- 3. 4325 --> 1A1B
- Please input a Four bit non repeating number,Q - quit the game:4367
- HISTORY:
- 1. 1234 --> 0A2B
- 2. 4321 --> 1A1B
- 3. 4325 --> 1A1B
- 4. 4367 --> 0A2B
- Please input a Four bit non repeating number,Q - quit the game:4389
- HISTORY:
- 1. 1234 --> 0A2B
- 2. 4321 --> 1A1B
- 3. 4325 --> 1A1B
- 4. 4367 --> 0A2B
- 5. 4389 --> 0A2B
- Please input a Four bit non repeating number,Q - quit the game:3498
- HISTORY:
- 1. 1234 --> 0A2B
- 2. 4321 --> 1A1B
- 3. 4325 --> 1A1B
- 4. 4367 --> 0A2B
- 5. 4389 --> 0A2B
- 6. 3498 --> 0A2B
- Please input a Four bit non repeating number,Q - quit the game:Q
- The answer is [8, 7, 2, 3]
- Quiting...
复制代码 |
评分
-
查看全部评分
|