Advertisement
fly51fly

Bagels

Nov 16th, 2021
594
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 1.36 KB | None | 0 0
  1. import random
  2. import os
  3. import time
  4.  
  5. def main():
  6.     ALL_DIGITS = '0123456789'
  7.     MAX_TRY = 6
  8.     NUM_DIGITS = 3
  9.  
  10.     os.system('clear')
  11.     print(f'我想好了一个{NUM_DIGITS}位不重复的数字,你来猜猜看?')
  12.  
  13.     target = ''.join(random.sample(ALL_DIGITS, k=NUM_DIGITS))
  14.     # print(target)
  15.  
  16.     round = 1
  17.     start_time = int(time.time())
  18.     while True:
  19.         while True:
  20.             guess = input('> ')
  21.             if len(guess) == NUM_DIGITS \
  22.                 and len(guess) == len(set(guess)) \
  23.                 and guess.isdigit():
  24.                 break
  25.             else:
  26.                 print(f'请输入{NUM_DIGITS}位不重复的数字')
  27.  
  28.         if guess == target:
  29.             print('猜对了!你真棒!')
  30.             end_time = int(time.time())
  31.             print(f'总共猜了{round}次,用时{end_time-start_time}秒')
  32.             break
  33.         else:
  34.             print(get_glue(target, guess))
  35.        
  36.         if round >= MAX_TRY:
  37.             print(f'您已经猜了{round}个数字,挑战失败!')
  38.             break
  39.  
  40.         round += 1
  41.  
  42. def get_glue(target, guess):
  43.     glue = ''
  44.     for index in range(len(guess)):
  45.         if guess[index] == target[index]:
  46.             glue += '✅'
  47.         elif guess[index] in target:
  48.             glue += '⭕️'
  49.         else:
  50.             glue += '❌'
  51.     glue = ''.join(sorted(list(glue)))
  52.     return glue
  53.  
  54. main()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement