Welcome to the Treehouse Community
Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.
Looking to learn something new?
Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.
Start your free trialCheri Castro
226 PointsI did some more work on this but still having issues with my Drivers license exam program, please help.
Posted this up yesterday but didn't get any input on it. Everything's still not working quite right. Your thoughts would be greatly appreciated!
def main():
submitted = ['A','D','C','A','C','A','D','C','A','D','A','C','B','D','A','C','C','A','D','B']
correct = ['1. B', '2. D', '3. A', '4. A', '5. C', '6. A', '7. B', '8. A', '9. C', '10. D', '11. B', '12. C', '13. D', '14. A', '15. D', '16. C', '17. C', '18. B', '19. D', '20. A']
correct_count = 0
incorrect_count = 0
results = getResults(submitted, correct, results)
def getResults(submitted, correct, results):
counter = 0
correct_count = 0
incorrect_count = 0
score = 0
while submitted == correct:
print ('Total Correct: ')
if submitted != correct:
print ('Total Incorrect: ')
for item in results: #***I'm stuck for what to put here(how to word it correctly),or if this is even how I should write it.
score[counter] = int(correct_count[counter])- int(incorrect_count[counter])
counter = counter + 1
print ('Score: ')
return results
def writeToFile(submitted):
submitted = ['A','D','C','A','C','A','D','C','A','D','A','C','B','D','A','C','C','A','D','B']
outfile = open('johns_answers.txt', 'w')
for item in submitted:
outfile.write(item + '\n')
outfile.close()
infile = open('johns_answers.txt', 'r')
submitted = infile.readlines()
infile.close()
sub_count = 0
while sub_count < len(submitted):
submitted[sub_count] = submitted[sub_count].rstrip('\n')
sub_count += 1
print ('The answers you submitted were: ', submitted)
print ('******************************************************************************************')
def writeToFile(correct):
correct = ['1. B', '2. D', '3. A', '4. A', '5. C', '6. A', '7. B', '8. A', '9. C', '10. D', '11. B', '12. C', '13. D', '14. A', '15. D', '16. C', '17. C', '18. B', '19. D', '20. A']
outfile = open('valid_answers.txt', 'w')
for item in correct:
outfile.write(item + '\n')
outfile.close()
infile = open('valid_answers.txt', 'r')
correct = infile.readlines()
infile.close()
correct_count = 0
while correct_count < len(correct):
correct[correct_count] = correct[correct_count].rstrip('\n')
correct_count += 1
print ('The correct answers are: ', correct)
print ('******************************************************************************************')
def writeToFile(results):
correct_count = 0
incorrect_count = 0
outfile = open('DMV.txt','w')
print (' Correct DMV answers Johns answers')
print ('Answer Correct/Incorrect')
for answer, correct in zip(submitted, correct):
outfile.write(item+ '\n')
outfile.close()
infile = open('DMV.txt', 'r')
getResults = infile.readlines()#I'm also stuck here, I'm nor sure that this is how this should be worded.
infile.close()
if answer == correct:#I'm not sure if I really need this here, with the def getResults section added above(I had this here originally but I've made some modifications, plus I think it's incorrectly
correct_count += 1 #wrote anyway, I was only getting incorrect results).
print (correct, answer ,' = Correct')
else:
answer != correct
incorrect_count += 1
print (correct, answer ,' = Incorrect')
main()
Cheri Castro
226 PointsRight now, because I changed some things, even after I posted this, nothing happens at all. When I had it this way, it was telling me that the results... were:
results = getResults(submitted, correct, results)
UnboundLocalError: local variable 'results' referenced before assignment
I have hashtagged things within the code, where I think my problems are.
Cheri Castro
226 PointsI had most of it working but the instructions state that it is supposed to be in the modular format, so I revised it to be in modules and now, nothing happens.
Cheri Castro
226 PointsI started out with it like this:
def main():
submitted = ['A','D','C','A','C','A','D','C','A','D','A','C','B','D','A','C','C','A','D','B']
outfile = open('johns_answers.txt', 'w')
for item in submitted:
outfile.write(item + '\n')
outfile.close()
infile = open('johns_answers.txt', 'r')
submitted = infile.readlines()
infile.close()
sub_count = 0
while sub_count < len(submitted):
submitted[sub_count] = submitted[sub_count].rstrip('\n')
sub_count += 1
print ('The answers you submitted were: ', submitted)
print ('******************************************************************************************')
correct = ['1. B', '2. D', '3. A', '4. A', '5. C', '6. A', '7. B', '8. A', '9. C', '10. D', '11. B', '12. C', '13. D', '14. A', '15. D', '16. C', '17. C', '18. B', '19. D', '20. A']
outfile = open('valid_answers.txt', 'w')
for item in correct:
outfile.write(item + '\n')
outfile.close()
infile = open('valid_answers.txt', 'r')
correct = infile.readlines()
infile.close()
correct_count = 0
while correct_count < len(correct):
correct[correct_count] = correct[correct_count].rstrip('\n')
correct_count += 1
print ('The correct answers are: ', correct)
print ('******************************************************************************************')
print (' Correct DMV answers Johns answers')
print ('Answer Correct/Incorrect')
correct_count = 0
incorrect_count = 0
number_questions = 0
for answer, correct in zip(submitted, correct):
if answer == correct:
correct_count += 1
print (correct, answer ,' = Correct')
else:
answer != correct
incorrect_count += 1
print (correct, answer ,' = Incorrect')
main()
The instructions state that is supposed to be modular though. That's why I changed it to the above. Also, I needed to add a third section that was supposed to write to a DMV.txt, which compared the two preset answers sections and then listed the results.
Cheri Castro
226 PointsAfter the top code kept coming back with the error, I changed this section, and then got no results so then I changed it again and still no results.
def main():
submitted = ['A','D','C','A','C','A','D','C','A','D','A','C','B','D','A','C','C','A','D','B']
correct = ['1. B', '2. D', '3. A', '4. A', '5. C', '6. A', '7. B', '8. A', '9. C', '10. D', '11. B', '12. C', '13. D', '14. A', '15. D', '16. C', '17. C', '18. B', '19. D', '20. A']
correct_count = 0
incorrect_count = 0
score = 0
def results(submitted, correct, results):
counter = 0
correct_count = 0
incorrect_count = 0
score = 0
while submitted == correct:
print ('Total Correct: ')
if submitted != correct:
print ('Total Incorrect: ')
for item in results: #***I'm stuck for what to put here(how to word it correctly),or if this is even how I should write it.
score[counter] = int(correct_count[counter])- int(incorrect_count[counter])
counter = counter + 1
print ('Score: ')
return results
2nd Change:
def results(submitted, correct, results):
counter = 0
correct_count = 0
incorrect_count = 0
score = 0
while submitted == correct:
print ('Total Correct: ')
if submitted != correct:
print ('Total Incorrect: ')
for item in results: #***I'm stuck for what to put here(how to word it correctly),or if this is even how I should write it.
score[counter] = int(correct_count[counter])- int(incorrect_count[counter])
counter = counter + 1
print ('Score: ')
return results
def writeToFile(results):
correct_count = 0
incorrect_count = 0
outfile = open('DMV.txt','w')
print (' Correct DMV answers Johns answers')
print ('Answer Correct/Incorrect')
for answer, correct in zip(submitted, correct):
outfile.write(item+ '\n')
outfile.close()
infile = open('DMV.txt', 'r')
getResults = infile.readlines()#I'm also stuck here, I'm nor sure that this is how this should be worded.
infile.close()
if answer == correct:#I'm not sure if I really need this here, with the def getResults section added above(I had this here originally but I've made some modifications, plus I think it's incorrectly
correct_count += 1 #wrote anyway, I was only getting incorrect results).
print (correct, answer ,' = Correct')
else:
answer != correct
incorrect_count += 1
print (correct, answer ,' = Incorrect')
main()
Results:
Python 3.6.3 (v3.6.3:2c5fed8, Oct 3 2017, 17:26:49) [MSC v.1900 32 bit (Intel)] on win32
Type "copyright", "credits" or "license()" for more information.
>>>
RESTART: C:/Users/0wner/Desktop/Programming logic/Python Labs/Final Project 8-9.py
>>>
2 Answers
Steven Parker
231,269 PointsFirst off, the term "hashtag" applies only to social media. What you have done in your program is "added comments" or just "commented" for short.
Then the error message "local variable 'results' referenced before assignment" is referring to this code on line 6:
results = getResults(submitted, correct, results)
This line is creating a new variable named "results" using the value returned from calling "getResults", but one of the parameters you are passing to that function is "results". All of the parameters must exist before the function is called, and the function must finish before the variable is created. So something is being done here out of order.
A bit of additional confusion occurs upon inspection of the "getResults" function code, where it seems that the function uses "results" as an iterable for a loop, but then nothing in the loop body uses any values from it. Perhaps this function should create the results but not require them as an argument? In that case, the loop might iterate on a range and use "counter" instead of "item".
Another puzzle is the three different functions all named "writeToFile". If these need to be separate, they should have different names. But none of them seem to be called from the main program.
You will probably want to review the overall program structure with the task requirements (which are not shown here).
Cheri Castro
226 PointsSteve, Oh, ok. Thank you for clarifying the hashtag term. I grew up with that symbol being referred to as a pound sign. So, switching to it being referred to as a hashtag still doesn't quite make sense to me, sorry about that. Yes the getResults is part of the problem but then, I changed it to just results and when that didn't really fix things either, I moved the whole module to just above where the results are actually needed. I can add the directions, maybe that would help. Perhaps, I'm making it all more complicated than it needs to be. I will try changing the item to counter, and remove the arguments.
Cheri Castro
226 PointsDriver’s License Exam The local driver’s license office has asked you to design a program that grades the written portion of the driver’s license exam. The exam has 20 multiple choice questions. Here are the correct answers:
- B6. A11. B16. C2. D7. B12. C17. C3. A8. A13. D18. B4. A9. C14. A19. D5. C10. D15. D20. A Your program should store these correct answers in an array. (Store each question’s correct answer in an element of a String array.) The program should ask the user to enter the student’s answers for each of the 20 questions, which should be stored in another array. After the student’s answers have been entered, the program should display a message indicating whether the student passed or failed the exam. (A student must correctly answer 15 of the 20 questions to pass the exam.) It should then display the total number of correctly answered questions, the total number of incorrectly answered questions, and a list showing the question numbers of the incorrectly answered questions.
(Gaddis 337) Gaddis, Tony. Starting Out with Programming Logic and Design, 4th Edition. Pearson, 20150219. VitalBook file. The citation provided is a guideline. Please check each citation for accuracy before use.
In addition, the instructor included the following requirements:
Complete Chapter 8 Programming Exercise 9, with the following modifications:
- Create a data file, valid_answers.txt, containing the correct answers listed in the problem statement.
- You will create a sample test file, johns_answers.txt, with the answers John submitted for the exam.
- Your program will read in both data files into two separate arrays. You will then compare each corresponding element to see if the matched. If there is a match, output the correct answer, and John's answer, followed by "Correct". If the answers do not match, output the correct answer, and John's answer, followed by "Incorrect". Space and align neatly. Create an output data file, titled DMV.txt for your results.
- Place your name and student ID at the top of the output file.
- The output should look similar to: Correct DMV Answer John's Answer Correct/Incorrect
- B B Correct
2. D A Incorrect
Total correct: XX Total incorrect: XX Score: XX %
Program Requirements:
- Design the program using Raptor.
- You MUST use Modular Programming techniques by using Sub Modules (Sub Charts in RAPTOR) in your program. Your "main" module should not be very large.
- Code the program successfully using Python.
- You MUST use Modular Programming techniques by using functions in your program. Your "main" function should not be very large. Other Requirements: • Documentation: Use the "Comments" feature to document each symbol in the flowchart. You do this by right-clicking the symbol and selecting "Comment." Be sure to identify the data type of each variable used. Be sure to explain what each formula does. Be sure to explain what each of the other symbols in the flowchart does in a comment. • Test and debug your Program: Create sample input data, run the program, then check your answers with a calculator or Excel. If something did not match up, then fix your program. • Program must execute and produce correct output. • Read this page again to be sure you covered all requirements. • See the Programming Project Rubric for grading principles.
Cheri Castro
226 PointsOh, the three modules all named writeToFile, I'm just confused here. I thought they all had to be named that, because that's what they're doing. I guess by what you said, it doesn't matter what they are named.? I'm fairly well out of my league here, the instructor doesn't interact with us much and just leaves us to our own devices to figure things out. I've figured out a great deal of it with help from treehouse but even other students in his class don't interact much. I've taken one of his classes previously and it was the same way then but in other teachers classes, people actually help each other through the use of the discussion boards.
Cheri Castro
226 PointsUnfortunately, making all the changes, didn't make any difference. Am I correct in thinking the original way that I had everything wrote out, when it was mostly working is not a good example of a modular program?
Steven Parker
231,269 PointsA "#" symbol is still a "pound sign" or "hash". A "hashtag" is "a word or phrase preceded by the symbol # that classifies or categorizes the accompanying text".
I understand your requirements better, and you clearly need to divide your program into modular sections, documented with "raptor", and implemented as separate functions in the Python.
You might want a single "writeFile" function to handle your output file, and perhaps a "readFile" function that you could call twice to handle the inputs. Note that moving your data into external files will make it even more difficult to share the program in a forum such as this.
You might try creating a "slack" channel for your class and inviting other students to join as a way of increasing communications within the class.
And if you have the time, the Python courses here cover the topics you would need to know to easily create a program with the requirements of this one.
Cheri Castro
226 PointsI've slowly been working my way through the Python classes but I can't get through them fast enough, before this class is over, on the 19th. I can't get to the Python part of the classes that would show me how to do all this, in a more efficient way, before the class ends. So, I've just been fumbling through most of it and will keep working on the Python classes on here to gain a better understanding of what I just went through, since the actual class doesn't offer that. The work requirements for the class have exceeded my time constraints to do both the class and also devote a decent amount of time to treehouse. I feel as if I learn more on treehouse than I do in my college class but I just can't get work, class and treehouse all done in the time frame that I am allotted currently. I don't know anything about a slack channel. The students in his class seem to think it is not allowed to collaborate on assignments. I don't think that's true. I think we should all help each other. Many of them don't seem to be willing to do that.
Cheri Castro
226 PointsSo, are you saying put all the writeto requirements in one module? and perhaps put all of the readfrom in two modules?
Steven Parker
231,269 PointsFrom a modular perspective, it might make sense to have a single function to handle writing the output file, and another one to handle reading in the the input file(s) and creating lists(s) (what your instructor is calling "arrays"). If you made this function take a filename argument and return the list, it could be called twice, once for each file, something like this:
# somewhere inside main() ...
correct_answers = readList("valid_answers.txt")
submitted_answers = readList("johns_answers.txt")
Cheri Castro
226 PointsOn the treehouse Python classes, I've been stuck on a- Use .split() and .join() challenge- for a few weeks now. I had to stop working at it for a long while so that I could get my homework assignments done, for my college class. I just tried working on it again earlier and got a tiny bit further but am stuck again. So, it's slow going on here, sometimes. I only started working on it again because I'm stuck on the drivers license exam, in my Python and my Raptor portions. I asked online to hire a tutor, in my local area, to help me get through these trouble areas, in my college class, but no one responded to my add. So...here I am just plugging away at it. I've made perfect grades on all my other assignments, for this term. It's just a twelve week class so it's pretty compressed.
Cheri Castro
226 PointsOk, I had tried combining the two already, after you said that but I didn't incorporate the suggestion that you just made. I'll try adding that in there now. So far, what I've tried hasn't made any difference. My other way of writing this all out, produced a result but when I saw that it needed to be modular, I had to revise everything and now, I can't get any result and I haven't been able to find any good examples online either.
Steven Parker
231,269 PointsThis task seems to be discussed on the web, but not necessarily implemented in Python. But it's possible you could get some ideas from the other language implementations. Some examples:
It also seems to be a topic on the commercial textbook/answer site Chegg Study, though my mention of it is not to be taken as an endorsement or recommendation.
Cheri Castro
226 PointsOk, thank you. I haven't worked in C++ yet. I did get some clues from dream in code, that's how I got this far, so far. I'm not a member of Chegg but I am a member of course hero which seems an utterly useless site.
Steven Parker
231,269 PointsCheri Castro — Normally, you would mark "best answer" as the answer that had the most information for resolving your issue. Marking your own comment as "best answer" might be confusing to other students reading the question.
Steven Parker
231,269 PointsSteven Parker
231,269 PointsA few more details beyond "not working quite right" might be helpful, so we know what you're expecting this to do and in what way it's failing.