2

现在我一直在尝试制作我自己版本的游戏线索,但我似乎无法找到如何做到这一点。我正在努力做到这一点,如果猜测是错误的,它会从其他猜测中给出提示,这不是真正的“杀手”,也不是他们之前的任何猜测。我将举一个类似的例子:

    a = input()
    b = input()
    c = input()
    d = input()
    e = input()
    f = input()
    hint = random.choice([a,b,c,d,e,f])
    hint != killer
    hint != previous_guess

现在我知道提示 != 杀手和提示 != previous_guess 并没有真正做任何事情,因为提示是之前分配的。我想知道是否有任何方法可以使它从不是杀手或先前猜测的变量中随机选择。提前致谢!哦,我还想指出我正在使用 python。

4

2 回答 2

1

您可以使用一组并减去您不想要的,例如

hint = random.choice(list({a, b, c, d, e, f} - {killer, previous_guess}))
于 2012-10-30T01:00:00.660 回答
0

这是您可以做到的基本方法。这使用了所谓的列表推导的概念,以便以有效的方式生成列表。.format()表示字符串格式,它允许您将变量传递到各种格式的字符串中(此处的文档,现在只知道{0}指的是 的第一个参数format())。

names使用上述列表推导生成,它在语法上等价于:

names = []
for i in range(6):
    names.append(raw_input('Enter a name: ')

此模式稍后用于生成您的列表,而无需杀手或先前的猜测。很高兴解释任何没有意义的部分(感谢@JonClements 指出我留下的一些奇怪之处):

import random


# Choose your names
names = [raw_input('Enter killer name: ') for i in xrange(6)]

# Choose a killer
killer = random.choice(names)

# Run for as many times as you want (here we do 6; could also be len(names)
max_guesses = 6

for guessno in xrange(max_guesses):
  guess = raw_input('Guess the killer: ')
  # If the guess is not the killer...
  if guess != killer:
    # This line creates a list that does not include the killer nor the guess
    hint = random.choice([n for n in names if n not in [killer, guess]])
    print 'Hint, the killer is not {0}'.format(hint)
  else:
    print 'Correct! The killer is {0}'.format(guess)
    break
于 2012-10-30T00:57:51.940 回答