Task:

• Create a text file with 5 questions in it.

• Read each question in from the file.

• Ask the user for their response.

• Create a new text file.

• Write the user’s answers to the text file.

• Ask the user if they’d like to review their answers. If yes, read in their answers from the text file. If no, end the program.

Here is my code...

# November 9, 2011
# A Few Questions

print "Please answer these questions."
print

few_Questions = open("few_questions.txt","r")

print few_Questions.readline()
Ques1 = raw_input ("")

print few_Questions.readline()
Ques2 = raw_input ("")

print few_Questions.readline()
Ques3 = raw_input ("")

print few_Questions.readline()
Ques4 = raw_input ("")

print few_Questions.readline()
Ques5 = raw_input ("")

few_Questions.close()

few_Questions = open("question_answers","w")
few_Questions.write(Ques1+"\t")
few_Questions.write(Ques2+"\t")
few_Questions.write(Ques3+"\t")
few_Questions.write(Ques4+"\t")
few_Questions.write(Ques5+"\t")

few_Questions.close()

answer = raw_input("Would you like to review your answers? Y or N.")

while answer == "Y":
    print few_Questions.readline()

My problems are as follows
1st the program doesn't run past my third question in my text file?
2nd I have to press enter twice to get my next question?
3rd cant figure out how to set up Y or N section. How do you set it up with a while loop?

Here are my questions...

What is your name?

What grade are you in?

What is your favorite color?

What is your favorite food?

Who is your favorite band?

Thanks in advance

Edited 5 Years Ago by Ezzaral: Fixed code tags

Use loop, do not repeat code.

answers = [raw_input(q)+'\n' for q in open('questions.txt')]
# save answers to file
with open('answers.txt', 'w') as out:
    out.writelines(answers)
    
if raw_input('Do you want to review your answers? ').lower() in ('yes', 'y'):
    with open('answers.txt') as inp:
        print inp.read()

questions.txt

What is your name?
What grade are you in?
What is your favorite color?
What is your favorite food?
Who is your favorite band?

Edited 5 Years Ago by pyTony: n/a

still having problems with the program...
Won't go past question three and checking answers section wrong
A simple while loop for this section will suffice but I don't know how to do this for this specific program

still having problems with the program...
Won't go past question three and checking answers section wrong

Dont' know what "Won't go past guestion three" means. On input, on writing, on checking answers? And have you already created the file "few_questions.txt" with the questions in it (that could be the cause of the "won't go past three" problem). You did not say. If not, that is the first thing your program must do. If you know functions, use one function to output the file the first time, a second function to read the file, ask for an answer, and write to the new file, etc. As stated before, use a loop:

print "Please answer these questions."
print

few_Questions = open("few_questions.txt","r")
few_responses = open("few_responses.txt","w")

## Your program does not store the question in the variable to use to write to new file
for rec in few_Questions:
    rec = rec.strip()     
    print rec
    response = raw_input ("Enter an answer: ")
    response = response.strip()
    few_responses.write("%s: %s\n" % (rec, response))    ## delimiter = colon
few_Questions.close()
few_responses.close()

Edited 5 Years Ago by woooee: n/a

This article has been dead for over six months. Start a new discussion instead.