0

好的。所以这就是我到目前为止所拥有的......#Russian Translation Program

import os
import random

#Asks users if they want to add more vocabulary
word_adder=raw_input("Add more words? If yes, press 1: ")
with open("Russian_study.txt","a") as f:
    while word_adder=="1":
        word=raw_input("Enter word: ")
        translation=raw_input("Word translation: ")
        f.write("{0}:{1},/n".format(word,translation))
        word_adder=raw_input("Add another word? If yes, press 1: ")

#Checks to see if file exists, if not one is created
with open("Russian_study.txt","a") as f:
    pass

os.system('clear')
print("Begin Quiz")

#Begin testing user
with open("Russian_study.txt","r") as f:
    from random import choice
    question, answer = choice(list(f)).split(':')
    result = raw_input('{0} is '.format(question))
    print('Correct' if result==answer else ':(')

该程序有效,但是,当添加多个条目时,它总是显示不正确。有什么帮助吗?此外,它在一个问题后停止运行,永远不会进入下一个问题......

4

2 回答 2

1

这里有几个问题。

  1. 一个错字:/n而不是\ninf.write("{0}:{1},/n"...

  2. 当您list(f)第一次在循环中执行时,它会调用f.readlines()将读取“指针”移动到文件末尾。因此,所有后续调用都list(f)将返回一个空列表。当心这种隐藏状态。

  3. list(f)在它返回的行中包含换行符,并且在任何答案的末尾都有一个逗号。所以answer得到类似的东西"word,\n"answer在与比较之前,您必须去掉这两个字符result

  4. 它在第一个问题后停止运行,因为您在提问部分没有循环。

  5. 另外,raw_inputPython3 中没有,只有input.

考虑到这一切,固定的程序(改动很小)可能如下所示:

import os
import random

#Asks users if they want to add more vocabulary
word_adder=input("Add more words? If yes, press 1: ")
with open("Russian_study.txt","a") as f:
    while word_adder=="1":
        word=input("Enter word: ")
        translation=input("Word translation: ")
        f.write("{0}:{1},\n".format(word,translation))
        word_adder=input("Add another word? If yes, press 1: ")

#Checks to see if file exists, if not one is created
with open("Russian_study.txt","a") as f:
    pass

os.system('clear')
print("Begin Quiz")

#Begin testing user
with open("Russian_study.txt","r") as f:
    l = list(f)
    from random import choice
    while True:
        question, answer = choice(l).split(':')
        answer = answer[:-2]
        result = input('{0} is '.format(question))
        print('Correct' if result==answer else ':( ')
于 2013-05-08T06:32:51.040 回答
0

对我有用的代码。感谢所有的帮助家伙

进口操作系统进口随机

#Asks users if they want to add more vocabulary
word_adder=raw_input("Add more words? If yes, press 1: ")
with open("Russian_study.txt","a") as f:
    while word_adder=="1":
    word=raw_input("Enter word: ")
    translation=raw_input("Word translation: ")
    f.write("{0}:{1},\n".format(word,translation))
    word_adder=raw_input("Add another word? If yes, press 1: ")

#Checks to see if file exists, if not one is created
with open("Russian_study.txt","a") as f:
    pass

os.system('clear')
print("Begin Quiz")

#Begin testing user
with open("Russian_study.txt","r") as f:
    l = list(f)
    from random import choice
    while True:
        question, answer = choice(l).split(':')
        answer = answer[:-2]
        result = raw_input('{0} is '.format(question))
        print('Correct' if result==answer else ':( ')
于 2013-05-08T06:48:13.613 回答