Reputation: 909
Ok. So this is what I have so far.... #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 ':(')
This program works, however, when multiple entries are added it always displays incorrect. Any help? Also, it stops running after one question, never gets to the next one....
Upvotes: 0
Views: 275
Reputation: 909
Code that worked for me. Thanks for all the help guys
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:
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 ':( ')
Upvotes: 0
Reputation: 9726
There are several problems here.
A typo: /n
instead of \n
in f.write("{0}:{1},/n"...
When you do list(f)
the first time in the loop, it calls f.readlines()
which moves the reading "pointer" to the end of the file. So all subsequent calls to list(f)
will return an empty list. Beware of this hidden state.
list(f)
includes the newline symbol in the lines it returns, plus you have a comma at the end of any answer. So answer
gets something like "word,\n"
. You have to strip these two characters before comparing answer
with result
.
It stops running after the first question because you do not have a loop in the questioning part.
Also, there's no raw_input
in Python3, just input
.
With all this in mind, the fixed program (with minimal changes) may look like:
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 ':( ')
Upvotes: 1