使用for循环检查变量中的每个数字到数组

时间:2018-04-02 10:15:22

标签: python python-2.7

背景故事:我正在创建一个猜谜游戏。用户必须输入4位数字,并与实际代码进行比较。程序输出正确位置正确猜测的位数以及错误位置的正确位数。当猜测中的数字与代码中的数字正确匹配时,数组varr将更改为False。

+------+--------+-----------+-----------+
|set_id|  abc_cd|abc_cnt_sum|cde_cnt_sum|
+------+--------+-----------+-----------+
|7057.0|abc00160|       11.0|        9.0|
|7057.0|abc00249|        0.0|        1.0|
+------+--------+-----------+-----------+

为此,我需要创建两个for循环。 “对于猜测中与代码匹配的每个数字......”和“对于数组中的剩余数字”

我如何用Python写这个?

3 个答案:

答案 0 :(得分:0)

我忽略了生成列表和用户输入部分的代码,只是为了显示核心代码:

from collections import Counter


def main():
    answers = [1, 2, 3, 4]
    guesses = [3, 5, 6, 4]

    answers_counter = Counter(answers)
    guesses_counter = Counter(guesses)
    diff_counter = answers_counter - guesses_counter
    number_of_all_correct_digits = len(answers) - len(diff_counter.values())

    number_of_correct_digits_in_correct_place = len(
        list(filter(lambda x: x == 0, [a - g for a, g in zip(answers, guesses)])))

    number_of_correct_digits_in_wrong_place = number_of_all_correct_digits - number_of_correct_digits_in_correct_place

    return number_of_correct_digits_in_correct_place, number_of_correct_digits_in_wrong_place


if __name__ == "__main__":
    r1, r2 = main()
    print(r1)
    print(r2)

并产生结果,正确位置的正确数字位数:1(数字3),错误位置的正确数字位数:1(数字4)

答案 1 :(得分:0)

也许尝试这样的事情:

import random

first_lock = random.randint(1, 50)
second_lock = random.randint(1, 50)
third_lock = random.randint(1, 50)
fourth_lock = random.randint(1, 50)

combination_dict = {
  'one': lambda one, cd, wd: (cd + 1, wd) if first_lock == one else (cd, wd + 1),
  'two': lambda two, cd, wd: (cd + 1, wd) if second_lock == two else (cd, wd + 1),
  'three': lambda three, cd, wd: (cd + 1, wd) if third_lock == three else (cd, wd + 1),
  'four':  lambda four, cd, wd: (cd + 1, wd) if fourth_lock == four else (cd, wd + 1)
}

def combination_lock(one, two, three, four):
  cd = 0
  wd = 0
  cd, wd = combination_dict['one'](one, cd, wd)
  cd, wd = combination_dict['two'](two, cd, wd)
  cd, wd = combination_dict['three'](three, cd, wd)
  cd, wd = combination_dict['four'](four, cd, wd)

  return cd, wd


def game_start():
  correct_digits = 0
  wrong_digits = 0

  locked = True
  while locked:
    correct_digits = 0
    wrong_digits = 0

    one = int(raw_input("First Lock: "))
    two = int(raw_input("Second Lock: "))
    three = int(raw_input("Third Lock: "))
    four = int(raw_input("Fourth Lock: "))

    correct_digits, wrong_digits = combination_lock(one, two, three, four)

    if (first_lock == one) and (second_lock == two) and (third_lock == three) and (fourth_lock == four):
      locked = False
      print "Congratz you escaped the locked room!"
    else:
      print "Right digits: ", str(correct_digits)
      print "Wrong digits: ", str(wrong_digits)

game_start()

答案 2 :(得分:0)

最简单且最易读的解决方案不需要几个 for循环

random_code = [2, 3, 4, 5]
user_code = [2, 5, 6, 7]
result = []
correct_guess = 0
correct_place = 0 
for i in xrange(4):
    if user_code[i] == random_code[i]:
        result.append(user_code[i])
        correct_place += 1
    else:
        result.append('X')
    if user_code[i] in random_code:
        correct_guess += 1

我添加了result作为直观表示。你的输出:

>>> correct_guess
2
>>> correct_place
1
>>> result
[2, 'X', 'X', 'X']

顺便说一句,您必须记住您的播放器代码与随机生成的代码具有相同长度的假设

这非常重要,因为它可能是导致错误的原因:D

你能做到吗

random_code.index(NUMBER)

如果它不在列表中,您将获得

ValueError: NUMBER is not in list

如果NUMBER将在列表中,您将获得其位置(在我们的情况下为0,1,2或3),那么您可以比较两个列表中元素的位置。

古德勒克!