我在用Python做一个问答游戏。到目前为止,程序读取一个文件。文件中的内容如下:
Poles|Magnet|?|Battery
这个游戏的工作原理是用户猜测“?”部分。你知道吗
通过在|
字符处拆分,我已经可以将字符串拆分为4个独立的部分,但现在我想在末尾添加以下内容:
Poles|Magnet|?|Battery/!Charge/Ends/Magic/Metal
我不知道如何让程序做到这一点:
我已经做到了:
# 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])
将第二个四分为另一个列表。
然后看哪一个在开头有一个!
(表示它是正确的答案),并把它放入一个字符串中,比如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
列表中去掉!
,这样当它被显示时就没有任何!
来表示答案了?你知道吗
您可以使用
re.split
:如果您只需要具有
!
的字符串:如果您需要知道它在哪个列表中:
这应该管用。首先,它被
|
分割。最后一个元素是你的答案。然后我们把答案从钥匙上取下来。现在我们把答案分成几个部分。最后,我们通过搜索!
得到正确答案,并将其保存在字符串answer
中。你知道吗输出
根据我的观点,如果我们能再加一个分隔符,一切都会很容易处理:D 注意:您有时可能有4个以上的选项
或者
在这种情况下,我们不认为如果你实现这样的游戏,你需要更多的帮助来解决这个问题。现在就这么简单了-
首先,用“-”分隔,得到2个列表。然后,用“|”拆分列表1,用“/”拆分列表2。:D个
另外,请加上评论,如果你还想解决以前的问题,我也可以试试。你知道吗
相关问题 更多 >
编程相关推荐