Python 翻译程序

Python 翻译程序,python,translation,Python,Translation,嗯。这就是我到目前为止得到的#俄语翻译程序 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

嗯。这就是我到目前为止得到的#俄语翻译程序

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 ':(')

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

这里有几个问题

  • 打字错误:
    /n
    而不是
    \n
    f.write(“{0}:{1},/n”…

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

  • list(f)
    在返回的行中包含换行符,并且在任何答案的末尾都有一个逗号。因此
    answer
    得到类似
    “word\n”
    。在将
    answer
    result
    进行比较之前,必须去掉这两个字符

  • 它在第一个问题之后停止运行,因为在提问部分没有循环

  • 另外,Python3中没有
    原始输入
    ,只有
    输入

  • 考虑到所有这些因素,固定程序(变化最小)可能如下所示:

    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 ':( ')
    

    对我有用的代码。谢谢大家的帮助

    导入操作系统 随机输入

    #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 ':( ')
    

    哈,你刚刚抢先一步。输入不起作用。它只在使用原始输入时运行。这很奇怪。我安装了Python 3.3.1,没有
    raw\u input()
    。此外,在中也没有提到
    raw\u input()