【发布时间】:2015-03-13 20:24:45
【问题描述】:
我正在用 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 列表中删除!,以便在显示时没有任何! 可以表示答案?
【问题讨论】: