2013-05-08 51 views
0

好的。因此,這是我迄今爲止.... #Russian翻譯課程翻譯計劃

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

這個程序工作,但是,當添加多個條目總是顯示不正確的。任何幫助?此外,它在一個問題後停止運行,從未進入下一個問題....

回答

1

這裏有幾個問題。

  1. 一個錯字:的\n/n代替f.write("{0}:{1},/n"...

  2. 當你做list(f)首次在循環中,它調用f.readlines()這將讀「指針」文件的末尾。因此,所有後續調用list(f)將返回一個空列表。謹防這種隱藏的狀態。

  3. list(f)在它返回的行中包含換行符號,再加上任何回答結尾的逗號。所以answer得到像"word,\n"。在比較answerresult之前,必須去掉這兩個字符。

  4. 在第一個問題後停止運行,因爲您在提問部分沒有循環。

  5. 另外,Python3中沒有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 ':(') 
+0

哈,你剛纔打我給它。 – Ewan 2013-05-08 06:34:59

+0

輸入,不起作用。它僅在使用raw_input時運行。 – h3tr1ck 2013-05-08 06:36:58

+0

這很奇怪。我安裝了Python 3.3.1,並沒有''raw_input()''那裏。另外,http://docs.python.org/3/library/functions.html中沒有提到''raw_input()''。 – fjarri 2013-05-08 06:39:59

0

代碼。感謝所有幫助傢伙

進口OS 進口隨機

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