每次满足条件语句时如何打印列表的一个(不同)部分?
How can I print one (different) part of a list every time a conditional statement is met?
所以我有一个功能,基本上是提示用户找到某个预定数字(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 '\n3[1;36;40m Fantastic! 76 is correct.'
elif not correct:
print '\n3[1;31;40mIncorrect. Here is a hint: 3[1;30;47m'
print hint_list[0]
try_and_respond()
保留一个计数器来决定接下来显示哪个提示。
如果你想获得随机顺序的提示,请使用 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 '\n3[1;36;40m Fantastic! 76 is correct.'
else:
if incorrect_count < len(hint_list):
print '\n3[1;31;40mIncorrect. Here is a hint: 3[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('\n3[1;36;40m Fantastic! 76 is correct.')
else:
if incorrect_count < len(hint_list):
print('\n3[1;31;40mIncorrect. Here is a hint: 3[1;30;47m')
print(hint_list[incorrect_count])
incorrect_count += 1
return correct
while not try_and_respond():
try_and_respond()
要循环一组值,通常很容易使用模 %
运算符。
尝试以下操作:
hint_idx = 0
while True:
print(hints[hint_idx])
hint_idx = (hint_idx + 1) % len(hints)
所以我有一个功能,基本上是提示用户找到某个预定数字(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 '\n3[1;36;40m Fantastic! 76 is correct.'
elif not correct:
print '\n3[1;31;40mIncorrect. Here is a hint: 3[1;30;47m'
print hint_list[0]
try_and_respond()
保留一个计数器来决定接下来显示哪个提示。
如果你想获得随机顺序的提示,请使用 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 '\n3[1;36;40m Fantastic! 76 is correct.'
else:
if incorrect_count < len(hint_list):
print '\n3[1;31;40mIncorrect. Here is a hint: 3[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('\n3[1;36;40m Fantastic! 76 is correct.')
else:
if incorrect_count < len(hint_list):
print('\n3[1;31;40mIncorrect. Here is a hint: 3[1;30;47m')
print(hint_list[incorrect_count])
incorrect_count += 1
return correct
while not try_and_respond():
try_and_respond()
要循环一组值,通常很容易使用模 %
运算符。
尝试以下操作:
hint_idx = 0
while True:
print(hints[hint_idx])
hint_idx = (hint_idx + 1) % len(hints)