如何更改此代码以跳过已选择的数字?

时间:2018-02-15 21:36:10

标签: python python-3.x python-2.7

这是一款游戏。猜猜代码的4位数,游戏输出正确位置的正确位数和错误位置的正确位数。

假设代码是4405并猜测4444 它应输出2,0,但输出2,2 从逻辑上讲,我希望程序不能与代码集中的数字正确匹配。如何更改此代码才能完成此操作?

  

假设arr是一个由4个falses组成的数组,而guess1,2,3,4是你猜测的4个数字

  wronPlace= 0
  if arr[0]== False:
      if guess1== code[1]:
          arr[0]== True
          wronPlace= (wronPlace+1)
      elif guess1== code[2]:
          arr[0]== True
          wronPlace= (wronPlace+1)
      elif guess1== code[3]:
          arr[0]== True
          wronPlace= (wronPlace+1)
  if arr[1]== False:
      if guess2== code[0]:
          arr[1]== True
          wronPlace= (wronPlace+1)
      elif guess2== code[2]:
          arr[1]== True
          wronPlace= (wronPlace+1)
      elif guess2== code[3]:
          arr[1]== True
          wronPlace= (wronPlace+1)
  if arr[2]== False:
      if guess3== code[0]:
          arr[2]== True
          wronPlace= (wronPlace+1)
      elif guess3== code[1]:    
          arr[2]== True
          wronPlace= (wronPlace+1)
      elif guess3== code[3]:
          arr[2]== True
          wronPlace= (wronPlace+1)
  if arr[3]== False:
     if guess4== code[0]:
         arr[3]== True
         wronPlace= (wronPlace+1)
     elif guess4== code[1]:
         arr[3]== True
         wronPlace= (wronPlace+1)
     elif guess4== code[2]:
         arr[3]== True
         wronPlace= (wronPlace+1)

2 个答案:

答案 0 :(得分:2)

您的代码比需要的代码复杂得多。您可以使用for循环来实现您想要的效果。

guess = ['1', '2', '3', '4']
answer = 2245

answer = str(answer)
corr_place, wrong_place = 0, 0
for i, g in enumerate(guess):
    if g in answer:
        if g == answer[i]:
            corr_place += 1
        else:
            wrong_place += 1

print("Correct position:", corr_place)
print("Wrong position:", wrong_place)


但更多Pythonic解决方案将使用列表推导:

guess = ['1', '2', '3', '4']
answer = 2245

answer = str(answer)
results = [g == d for g, d in zip(guess, answer) if g in answer]
corr = sum(results)

print("Correct position:", corr)
print("Wrong position:", len(results) - corr)

答案 1 :(得分:2)

from random import choice
from string import digits
from textwrap import dedent


def get_four_digit_number(msg):
    while True:
        try:
            num = int(input(msg))
        except ValueError:
            pass
        else:
            if 1000 <= num <= 9999:
                return str(num)


if __name__ == '__main__':
    secret_code = ''.join(choice(digits) for _ in range(4))

    while True:
        guess = get_four_digit_number('Enter your 4-digit guess: ')
        correct = []
        incorrect = []
        for a, b in zip(guess, secret_code):
            if a == b:
                correct.append('^')
                incorrect.append(' ')
            else:
                correct.append(' ')
                incorrect.append('!')

        print(dedent("""
                   {} <-- Incorrect
            Guess: {}
                   {} <-- Correct
        """.format(''.join(incorrect), guess, ''.join(correct))))

        if correct == list('^^^^'):
            print('Nice guess! The secret code was {}'.format(secret_code))
            break

输出示例:

Enter your 4-digit guess: 1111

       !!!! <-- Incorrect
Guess: 1111
            <-- Correct

Enter your 4-digit guess: 2222

       !!!! <-- Incorrect
Guess: 2222
            <-- Correct

Enter your 4-digit guess: 3333

        !!  <-- Incorrect
Guess: 3333
       ^  ^ <-- Correct

Enter your 4-digit guess: 3443

        !!  <-- Incorrect
Guess: 3443
       ^  ^ <-- Correct

Enter your 4-digit guess: 3553

        !!  <-- Incorrect
Guess: 3553
       ^  ^ <-- Correct

Enter your 4-digit guess: 3663

        !!  <-- Incorrect
Guess: 3663
       ^  ^ <-- Correct

Enter your 4-digit guess: 3773

        !!  <-- Incorrect
Guess: 3773
       ^  ^ <-- Correct

Enter your 4-digit guess: 3883

        !!  <-- Incorrect
Guess: 3883
       ^  ^ <-- Correct

Enter your 4-digit guess: 3993

         !  <-- Incorrect
Guess: 3993
       ^^ ^ <-- Correct

Enter your 4-digit guess: 3903

            <-- Incorrect
Guess: 3903
       ^^^^ <-- Correct

Nice guess! The secret code was 3903