python 翻译计划

o7jaxewo  于 2023-02-18  发布在  Python
关注(0)|答案(2)|浏览(122)

好的。这就是我目前所拥有的......#俄语翻译程序

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

这个程序可以工作,但是,当添加多个条目时,它总是显示不正确。有帮助吗?而且,它在一个问题后停止运行,永远不会进入下一个问题。

6vl6ewon

6vl6ewon1#

这里有几个问题。
1.排印错误:/n代替f.write("{0}:{1},/n"...中的\n
1.当你在循环中第一次执行list(f)时,它会调用f.readlines(),将阅读“指针”移动到文件末尾,所以所有后续的list(f)调用都会返回一个空列表,注意这种隐藏状态。

  1. list(f)在它返回的行中包含换行符,并且在任何答案的末尾都有一个逗号,所以answer得到类似"word,\n"的结果,在比较answerresult之前,必须去掉这两个字符。
    1.它在第一个问题后停止运行,因为在提问部分没有循环。
    1.另外,Python 3中没有raw_input,只有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 ':( ')
mcdcgff0

mcdcgff02#

代码对我有用。谢谢所有的帮助
导入操作系统导入随机

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

相关问题