Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8468291
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 10, 20262026-06-10T15:53:37+00:00 2026-06-10T15:53:37+00:00

I’m new to Python (and also to stackoverflow, as you will easily notice it

  • 0

I’m new to Python (and also to stackoverflow, as you will easily notice it !)

I’m actually trying to write a program that would work as follow :
the user launch the program.
he’s being asked whether he wants to enter a new word, and the translation of that word.
The word and its translation are stored in a file (data.txt).
When he’s done adding new words, the quiz starts.
The program pick a word, and ask the user for the translation. If the answer is similar to the translation in the file, the program returns “Great !”, if not, it prints the correct answer.

As you can see, it’s pretty simple. My problem here is working with the file, especially retrieving what is inside the file and using it correctly.

Here is my code :

#!/usr/bin/python3.2
# -*-coding:Utf-8 -*

#Vocabulary/translation quiz

import os
import random

keep_adding=input("Would you like to add a new word ? If yes, press \"O\" : ")
while keep_adding=="O":
    entry=[]
    word=input("Enter a word : ")
    word=str(word)
    entry.append(word)
    translation=input("And its translation : ")
    translation=str(translation)
    entry.append(translation)
    entry=str(entry)
    f = open("data.txt","a")
    f.write(entry)
    f.close()
    keep_adding=input("To continue, press \"O\" : ")

f = open("data.txt","a") #in case the file doesn't exist, we create one
f.close()

os.system('clear')
print("* * * QUIZ STARTS ! * * *")

f = open("data.txt","r")

text = f.readlines()
text = list(text)
print("What is the translation for : ",text[0], "?")
answer = input("Answer : ")
if (answer == text[1]):
    print("Congratulations ! That's the good answer !")
else:
    print("Wrong. The correct answer was : ",text[1])

Thanks a lot for your help !

EDIT : did bring some corrections to my code.
What I get is the following :

    * * * QUIZ STARTS ! * * *
What is the translation for :  ['alpha', 'bravo']['one', 'two']['x', 'y'] ?
Answer : alpha
Traceback (most recent call last):
  File "Python_progs/voc.py", line 43, in <module>
    if (answer == text[1]):
IndexError: list index out of range

and in my file, I have this :

['alpha', 'bravo']['one', 'two']['x', 'y']

So actually, I would like to get only the first word in the question (i.e. alpha) and when answering bravo, having it right.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-10T15:53:38+00:00Added an answer on June 10, 2026 at 3:53 pm

    The problem

    You’re main issue is the way you’re storing/retrieving things in the quiz file.

    You’re doing f.write(str(entry)), which is writing the string representation of the entry. I’m not quite sure what your intent is here, but you should realize two things: 1) str representations of lists are tricky to turn back into lists (when reading the file) and 2) write() doesn’t append newlines at the end. If you were to do:

    f.write("line1")
    f.write("line2")
    f.write("line3")
    

    Then your file would look like this:

    line1line2line3
    

    Anyway, everything is saved on one line, so when you do f.readlines(), this returns an object like so:

    ["['alpha', 'bravo']['one', 'two']['x', 'y']"]
    

    Or more generally:

    [a_string,]
    

    As you can see, it’s a list with only one item in it. That’s why you get an error when you do

    if (answer == text[1]):   
    

    You’re trying to access a second item that doesn’t exist.

    The solution?

    What you need to do is store each quiz/answer pair as a separate line, with a specific delimiter separating the quiz and answer:

        quiz, answer
        alpha, bravo
        one, two
        etc...
    

    For example:

    with open("myquizfile.txt", "w") as f:
        while keepGoing: #You'd have to add your own exit logic.
            question = input("Enter a question: ")
            answer = input("Enter an answer: ")
            f.write("{0},{1}\n".format(question, answer)) #Notice the newline, \n
    

    And to read this file, you’d do something like this:

    with open("myquizfile.txt", "r") as f:
        question_answer_pairs = [line.split(",") for line in f]
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm parsing an RSS feed that has an &#8217; in it. SimpleXML turns this
I need a function that will clean a strings' special characters. I do NOT
I'm trying to create an if statement in PHP that prevents a single post
I'm new to using the Perl treebuilder module for HTML parsing and can't figure
link Im having trouble converting the html entites into html characters, (&# 8217;) i
That's pretty much it. I'm using Nokogiri to scrape a web page what has
I would like to count the length of a string with PHP. The string
I am trying to understand how to use SyndicationItem to display feed which is
I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function
I've got a string that has curly quotes in it. I'd like to replace

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.