Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/284.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 如何在每次满足条件语句时打印列表的一个(不同)部分?_Python_List_Python 2.7_Loops - Fatal编程技术网

Python 如何在每次满足条件语句时打印列表的一个(不同)部分?

Python 如何在每次满足条件语句时打印列表的一个(不同)部分?,python,list,python-2.7,loops,Python,List,Python 2.7,Loops,因此,我有一个功能,基本上提示用户找到某个预定的数字(76/76)。每次做出错误的猜测时,他们都会得到提示,然后选择再次猜测。对于每一个错误的猜测,我希望能够从我的列表中给他们不同的提示。这也是一个棘手的问题,因为我必须有一系列的提示,所以在列表用尽后,应该提示用户在没有指导的情况下再次猜测。我还没有提出我的提示,但下面是代码: hint_list = ['filler_1','filler_2', 'filler_3', 'filler_4', 'filler_5'] def try_and

因此,我有一个功能,基本上提示用户找到某个预定的数字(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()

保留一个计数器以决定下一步显示哪个提示

如果您希望以随机顺序显示提示,请使用random.shuffle()

#导入随机#如果随机提示顺序,则取消注释
提示列表=['filler_1'、'filler_2'、'filler_3'、'filler_4'、'filler_5']
#取消注释随机提示顺序的下一行
#随机移动(提示列表)
计数不正确=0
def try_和_response():
全局错误计数
猜测=原始输入(“猜测:”)
如果guess.isdigit():
猜测=int(猜测)
正确=猜测==76
其他:
correct=猜测。lower()=“七十六”
如果正确:
打印“\n\033[1;36;40m!76正确。”
其他:
如果计数不正确
这里是一个没有递归的修改版本,而是一个显式循环和一些代码,使代码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()

#导入随机#如果随机提示顺序,则取消注释
导入系统
#现在是2020年,所以我建议
#使代码也与python 3兼容。
如果系统版本信息[0]<3:
输入=原始输入
提示列表=['filler_1'、'filler_2'、'filler_3'、'filler_4'、'filler_5']
#取消注释随机提示顺序的下一行
#随机移动(提示列表)
计数不正确=0
def try_和_response():
全局错误计数
猜测=输入(“猜测:”)
如果guess.isdigit():
猜测=int(猜测)
正确=猜测==76
其他:
correct=猜测。lower()=“七十六”
如果正确:
打印(“\n\033[1;36;40m!76是正确的。”)
其他:
如果计数不正确
要循环一组值,通常很容易使用模
%
运算符

尝试以下方法:

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

是随机的?还是简单地从一开始就按相同的顺序循环?@orlp相同的顺序?两个答案中是否没有一个是令人满意的?如果没有一个是令人满意的,请发表评论,以便您可以得到更好的答案。如果您有令人满意的答案,请向上投票/接受。如果太多声誉较低的用户不接受回答,那么就有风险,越来越多的人将不会帮助名声不好的用户,这将是一个令人遗憾的提示,你的代码“有点奇怪”idt不要使用循环,而是递归来提示我的答案中的更多次我只是按照提示列表的方式处理,不要碰代码的其余部分。我可能会编辑我的ans为了增加你的脚本的修改版本,我认为更多的普通替换:<代码>打印HistyList[ 0 ] <代码>打印HistyList[InOrthtTyTalk] < /Cord>我的BRead读取你的答案到@ OrLP,如果你没有随机的顺序,停止给出提示,然后删除“<代码>导入随机< /代码>和<代码>随机化。(提示列表)
在我的回答中,我认为OP要求显示每个提示,然后停止给出提示。不过显示模运算符仍然很好。阅读此问题的其他人可能希望循环浏览。
hint_idx = 0
while True:
    print(hints[hint_idx])
    hint_idx = (hint_idx + 1) % len(hints)