通过三个分隔符将字符串分割,并将它们添加到不同的列表中

2024-09-28 22:24:34 发布

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

我在用Python做一个问答游戏。到目前为止,程序读取一个文件。文件中的内容如下:

Poles|Magnet|?|Battery

这个游戏的工作原理是用户猜测“?”部分。你知道吗

通过在|字符处拆分,我已经可以将字符串拆分为4个独立的部分,但现在我想在末尾添加以下内容:

Poles|Magnet|?|Battery/!Charge/Ends/Magic/Metal

我不知道如何让程序做到这一点:

  1. 将前四项分成一个列表。你知道吗

我已经做到了:

# Read questions in from a file.
with open("questions.txt", "r") as questions:
    data = questions.read().split("|")
    for x in range(0,4):
        print(data[x])
  1. 将第二个四分为另一个列表。

  2. 然后看哪一个在开头有一个!(表示它是正确的答案),并把它放入一个字符串中,比如answer。然后用户的输入就可以进行测试。

以下是目前为止的全部情况:

# Quick Thinker!

import os

def buildQuestions():
# Read questions in from a file.
with open("questions.txt", "r") as questions:
    data = questions.read().split("|")
    for x in range(0,4):
        print(data[x])


def intro():
print("        ____        _      _      _______ _     _       _             _ ")
print("       / __ \      (_)    | |    |__   __| |   (_)     | |           | |")
print("      | |  | |_   _ _  ___| | __    | |  | |__  _ _ __ | | _____ _ __| |")
print("      | |  | | | | | |/ __| |/ /    | |  | '_ \| | '_ \| |/ / _ \ '__| |")
print("      | |__| | |_| | | (__|   <     | |  | | | | | | | |   <  __/ |  |_|")
print("        \___\_\\__,_|_|\___|_|\_\    |_|  |_| |_|_|_| |_|_|\_\___|_|  (_)")
print("                                                                  ")
input("")
os.system("cls")
print("Welcome to the game!")
print("In QUICKTHINKER, you must select the correct missing entry from a sequence.")
print("")
print("For Example: ")
print("")
print("PEACE | CHAOS | CREATION | ?")
print("")
print("A: MANUFACTURE")
print("B: BUILD")
print("C: DESTRUCTION")
print("")
print("The correct answer for this would be DESTRUCTION.")
print("")
print("There will be, more or less, no pattern between questions.")
print("Each have their own logic.")
input("")
os.system("cls")

intro()
buildQuestions()
input("")

谢谢你的帮助。 谢谢

编辑

我不知道是否要把它变成另一个问题,但是我该如何从answers列表中去掉!,这样当它被显示时就没有任何!来表示答案了?你知道吗


Tags: 文件infrom程序游戏列表forinput
3条回答

您可以使用re.split

s = "Poles|Magnet|?|Battery/!Charge/Ends/Magic/Metal"

spl = re.split("[|/]",s)
a,b = spl[:4],spl[4:]
print(a,b)
(['Poles', 'Magnet', '?', 'Battery'], ['!Charge', 'Ends', 'Magic', 'Metal'])

如果您只需要具有!的字符串:

answer = next(x for x in spl if x.startswith("!"))
print(answer)
!Charge

如果您需要知道它在哪个列表中:

answer_a,answer_b = next((x for x in a if x.startswith("!")),""), next((x for x in b if x.startswith("!")),"")
print("The answer {} was in list1".format(answer_a) if answer_a else "The answer {} was in list2".format(answer_b))

The answer !Charge was in list2

这应该管用。首先,它被|分割。最后一个元素是你的答案。然后我们把答案从钥匙上取下来。现在我们把答案分成几个部分。最后,我们通过搜索!得到正确答案,并将其保存在字符串answer中。你知道吗

with open("questions.txt", "r") as questions:
    keys = questions.read().split('|')
    answers = keys[3]
    keys[3] = keys[3].split('/', 1)[0]

    answers = answers.split('/')[1:]

    answer = [x for x in answers if '!' in x][0].strip('!')

    answers = [x.strip('!') for x in answers]

    print(keys)
    print(answers)
    print(answer)

输出

['Poles', 'Magnet', '?', 'Battery']
['Charge', 'Ends', 'Magic', 'Metal']
Charge

根据我的观点,如果我们能再加一个分隔符,一切都会很容易处理:D 注意:您有时可能有4个以上的选项

Poles|Magnet|?|Battery-!Charge/Ends/Magic/Metal 

或者

Poles|Magnet|?|Battery-Ends/!Charge/Magic/Metal

在这种情况下,我们不认为如果你实现这样的游戏,你需要更多的帮助来解决这个问题。现在就这么简单了-

首先,用“-”分隔,得到2个列表。然后,用“|”拆分列表1,用“/”拆分列表2。:D个

另外,请加上评论,如果你还想解决以前的问题,我也可以试试。你知道吗

相关问题 更多 >