Python中的智力竞赛游戏

2024-09-28 01:25:33 发布

您现在位置:Python中文网/ 问答频道 /正文

在设计智力竞赛游戏时需要一些帮助。我用Python编写了以下内容:一个字符串和一个列表。在

quiz_string = '''there are two types of loops in programming.  The blank1 loop and the blank2 loop'''

answer_list = ['for', 'while']

用户将被提示如下:什么应该在空白1?在

如果用户回答“for”,则questing_string变量中的“blank1”将替换为answer_list变量中的“for”。对blank2重复此过程。在

如果有任何关于代码的帮助,我们将不胜感激。在


Tags: 字符串用户answerloop游戏列表forstring
2条回答

string.replace()可能会有帮助。在

例如:

quiz_string = '''there are two types of loops in programming.  The blank1 loop and the blank2 loop'''
answer_list = ['for', 'while']

blank1 = input('First Blank ')
blank2 = input('Second Blank ')

if blank1 in answer_list :
    quiz_string = quiz_string.replace('blank1', blank1)
if blank2 in answer_list :
    quiz_string = quiz_string.replace('blank2', blank2)

print(quiz_string)

您可以根据需要修改验证。在

可以使用带适当变量的格式字符串:

quiz_string = '''there are two types of loops in programming.  The {} loop and the {} loop'''
guesses = ['blank1', 'blank2']
print(quiz_string.format(*guesses))

当用户输入正确的答案时,您将用用户的输入替换guesses中的元素,并再次进行相同的print调用。然后它将用guesses中的新值替换{}。在

相关问题 更多 >

    热门问题