如何使用文本文件进行测验?

问题描述 投票:0回答:1

我尝试使用提供的文本文件创建一个翻译测验游戏程序。该文件的每一行包含 50 个毛利语单词及其英文翻译。该程序旨在从文本文件中随机抽取十个毛利语单词,并询问用户其英文翻译。应该有两种不同的测验模式,并且不应该区分大小写。

print ("Welcome to the Te Reo Maori Quiz!")
print ("Created by Emanuel Folorunsho")

with open ("Maori2Eng.txt") as e:
    for c in e:

我输入了上面的代码,但不确定还要添加什么。

python file input text-files translate
1个回答
0
投票

主.py

import random

with open('text.txt') as file:
    word_list = (file.readlines())

    selected = random.choice(word_list).split(':')

    question = selected[0]
    answer = selected[1]

    print(question)
    print(answer)

文本.txt

Question:Answer
Question2:Answer2
Question3:Answer3
Question4:Answer4
Question5:Answer5

输出

Question5
Answer5

我不确定您的

.txt
文件的格式如何,但您可以使用
.split()
来分隔您的问题和答案

© www.soinside.com 2019 - 2024. All rights reserved.