【问题标题】:How can I print one (different) part of a list every time a conditional statement is met?每次满足条件语句时,如何打印列表的一个(不同)部分?
【发布时间】:2020-12-04 00:00:30
【问题描述】:

所以我有一个功能,基本上提示用户找到某个预定的数字(76/76)。每次做出不正确的猜测时,都会给他们一个提示,然后选择再次猜测。对于每个错误的猜测,我希望能够从我的列表中为他们提供不同的提示。这也很棘手,因为我必须有一定数量的提示,所以在列表用完后,应该提示用户在没有指导的情况下再次猜测。我还没有想出我的提示,但这里是代码:

hint_list = ['filler_1','filler_2', 'filler_3', 'filler_4', 'filler_5']

def try_and_respond():
    guess = raw_input('Guess: ')
    if guess.isdigit():
        guess = int(guess)
        correct = guess == 76
    else:
        correct = guess.lower() == 'seventy-six'
    if correct:
        print '\n\033[1;36;40m Fantastic! 76 is correct.'
    elif not correct:
        print '\n\033[1;31;40mIncorrect. Here is a hint: \033[1;30;47m'
        print hint_list[0]
        try_and_respond()

【问题讨论】:

  • 应该是随机的吧?还是简单地从头开始循环,以相同的顺序?
  • @orlp 相同顺序
  • 这两个答案没有一个是令人满意的吗?如果没有满意的请评论,以便您获得更好的答案。如果您有满意的答案,请点赞/接受。如果太多低声誉的用户不接受答案,那么就有越来越多的人不会帮助低声誉的用户的风险,这将是一个遗憾

标签: python list python-2.7 loops


【解决方案1】:

保留一个计数器来决定接下来显示哪个提示。

如果您想以随机顺序获得提示,请使用 random.shuffle()

# import random  # uncomment if random hint order
hint_list = ['filler_1','filler_2', 'filler_3', 'filler_4', 'filler_5']

# uncomment next line for random hint order
# random.shuffle(hint_list)

incorrect_count = 0
def try_and_respond():
    global incorrect_count
    guess = raw_input('Guess: ')
    if guess.isdigit():
        guess = int(guess)
        correct = guess == 76
    else:
        correct = guess.lower() == 'seventy-six'
    if correct:
        print '\n\033[1;36;40m Fantastic! 76 is correct.'
    else:
        if incorrect_count < len(hint_list):
            print '\n\033[1;31;40mIncorrect. Here is a hint: \033[1;30;47m'
            print hint_list[incorrect_count]
            incorrect_count += 1
        try_and_respond()

try_and_respond()

这里是一个没有递归的修改版本,而是一个显式循环和一些代码来使代码兼容python3,但让它仍然与python2一起运行

# import random  # uncomment if random hint order
import sys

# It's 2020, So I suggest to
# make the code also python 3 compatible.
if sys.version_info[0] < 3:
    input = raw_input

hint_list = ['filler_1','filler_2', 'filler_3', 'filler_4', 'filler_5']

# uncomment next line for random hint order
# random.shuffle(hint_list)

incorrect_count = 0
def try_and_respond():
    global incorrect_count
    guess = input('Guess: ')
    if guess.isdigit():
        guess = int(guess)
        correct = guess == 76
    else:
        correct = guess.lower() == 'seventy-six'
    if correct:
        print('\n\033[1;36;40m Fantastic! 76 is correct.')
    else:
        if incorrect_count < len(hint_list):
            print('\n\033[1;31;40mIncorrect. Here is a hint: \033[1;30;47m')
            print(hint_list[incorrect_count])
            incorrect_count += 1
    return correct

while not try_and_respond():
    try_and_respond()

【讨论】:

  • 请注意,您的代码“有点奇怪” Idt不使用循环,但递归提示更多时间在我的回答中,我只是解决了提示列表的方式并且没有触摸其余的代码。我可能会稍后编辑我的答案以添加您的脚本的修改版本,我认为这更常见
  • 刚刚替换:print hint_list[0]print hint_list[incorrect_count] 我的错
  • 只需阅读您对@orlp 的回答如果您不想要随机顺序并停止提供提示,那么只需从我的回答中删除import randomrandom.shuffle(hint_list)
【解决方案2】:

要循环一组值,通常很容易使用模 % 运算符。

尝试以下方法:

hint_idx = 0
while True:
    print(hints[hint_idx])
    hint_idx = (hint_idx + 1) % len(hints)

【讨论】:

  • 我认为 OP 要求显示每个提示,然后停止给出提示。不过,展示模运算符仍然很好。阅读此问题的其他人可能想要循环浏览。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2019-02-09
  • 1970-01-01
  • 2014-04-09
  • 2018-01-16
相关资源
最近更新 更多