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 8935699
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 15, 20262026-06-15T10:03:14+00:00 2026-06-15T10:03:14+00:00

I can not solve a problem in python. the code opens a text file

  • 0

I can not solve a problem in python.
the code opens a text file where are the questions.
how to put the correct answer for each question?

cod:

import random
file = open('/home/jonny/questions.txt', 'r')
text = file.read()
separator = '*'
questions = text.split(separator)
print random.choice(questions)

attempt:

resp=raw_input('answer: ')
for question[0] in questions:
if resp=='a':
print 'gratzz'

txt:

*how are you?
a) well
b) badly
c) do not know
*What are your plans for today
a) nothing
b) something
c) do not know
  • 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-15T10:03:16+00:00Added an answer on June 15, 2026 at 10:03 am

    Try storing your data in a standard structured format such as JSON. A library for parsing JSON is included with Python.

    Create a file called questions.json containing:

    {
      "What are your plans for today?": {
        "answers": ["nothing", "something", "do not know"],
        "correct_answer": 2
      },
      "how are you?": {
        "answers": ["well", "badly", "do not know"],
        "correct_answer": 1
      }
    }
    

    Then you can parse it and ask the user a random question like this:

    import json
    import random
    
    # read data from JSON file
    questions = json.load(open("questions.json"))
    
    # pick a random question
    question = random.choice(questions.keys())
    
    # get the list of possible answers and correct answer for this question
    answers = questions[question]['answers']
    correct_answer = questions[question]['correct_answer']
    
    # display question and list of possible answers    
    print question
    for n, answer in enumerate(answers):
        print "%d) %s" % (n + 1, answer)
    
    # ask user for answer and check if it's the correct answer for this question
    resp = raw_input('answer: ')
    if resp == str(correct_answer):
        print "correct!"
    else:   
        print "sorry, the correct answer was %s" % correct_answer
    

    Example output:

    What are your plans for today?
    1) nothing
    2) something
    3) do not know
    answer: 3
    sorry, the correct answer was 2
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm using python 2.6.6 and I can't solve my problem. I have this code:
there's this interesting problem i can not solve myself. I will be very glad,
I can't solve this problem... I'm having some problems with fetched properties (not with
Sorry about the stupid question however I can't see/ not good enough to solve
I am trying to solve a problem /implementation using xpath. Using code (python) is
How can I solve this problem Cannot Create Forms. No MDI forms are currently
How can i solve must declare a body because it is not marked abstract,
I'm trying to solve a two-dimensional random walk problem from the book, exploring python.
I looked all over for the answer to my question and could not find
I wasted most of my morning failing to solve this simple problem. Using python,

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.