Non-programmer tutorial, stuck at list exercise

Reply

Join Date: Sep 2006
Posts: 109
Reputation: chris99 is an unknown quantity at this point 
Solved Threads: 0
chris99's Avatar
chris99 chris99 is offline Offline
Junior Poster

Non-programmer tutorial, stuck at list exercise

 
0
  #1
Sep 1st, 2006
It is very annoying, I have typed it as best I can, but option 2 doesn't work. Option 1 is to take the test in Test.py, option 9 is to quit, and option 2 is supposed to print all the questions and answers. This is the code:

  1. true = 1
  2. false = 0
  3. def get_questions():
  4. return[["What colour is the daytime sky on a clear day?","blue"],\
  5. ["What is the answer to life, the universe and everything?","42"],\
  6. ["What is a three letter word for mouse trap?","cat"],\
  7. ["What noise does a truly advanced machine make?","ping"]]
  8. menu_item = 0
  9. while menu_item != 9:
  10. print "------------------------------------"
  11. print "1. Take test"
  12. print "2. Display all questions and answers."
  13. print "9. quit"
  14. menu_item = input("Pick an item from the menu: ")
  15. if menu_item == 1:
  16. def check_question(question_and_answer):
  17. question = question_and_answer[0]
  18. answer = question_and_answer[1]
  19. given_answer = raw_input(question)
  20. if answer == given_answer:
  21. print "Correct"
  22. return true
  23. else:
  24. print "Incorrect, correct was: ",answer
  25. return false
  26. def run_test(questions):
  27. if len(questions) == 0:
  28. print "No questions were given."
  29. return
  30. index = 0
  31. right = 0
  32. while index < len(questions):
  33. if check_question(questions[index]):
  34. right = right + 1
  35. index = index + 1
  36. print "You got ",right*100/len(questions),"% right out of",len(questions)
  37. run_test(get_questions())
  38.  
  39. elif menu_item == 2:
  40. current = 0
  41. if len(get_questions) > current:
  42. while current < len(get_questions(index)):
  43. index=current
  44. print current,". ",get_questions(current)
  45. current = current + 1
  46. else:
  47. print "No questions"
  48. print "Goodbye."

Here is the error message when I type "2" into the option selection:

Traceback (most recent call last):
File "C:\Python24\test", line 45, in -toplevel-
print current,". ",get_questions(current)
TypeError: get_questions() takes no arguments (1 given)
What is wrong with my program and how do I fix it?
Reply With Quote Quick reply to this message  
Join Date: Aug 2005
Posts: 148
Reputation: Micko is on a distinguished road 
Solved Threads: 6
Micko Micko is offline Offline
Junior Poster

Re: Non-programmer tutorial, stuck at list exercise

 
0
  #2
Sep 1st, 2006
Well, you have defined function get_questions() not to take any arguments, but in lines:
  1. while current < len(get_questions(index)):
and
  1. print current,". ",get_questions(current)
you're passing argument to it.
If you want your function to deal with arguments, you need to define it that way.

I would do something like this ( I reserve right to be completely wrong ):
[php]
true = 1
false = 0
def get_questions(x = -1):
lista = [["What colour is the daytime sky on a clear day?","blue"],\
["What is the answer to life, the universe and everything?","42"],\
["What is a three letter word for mouse trap?","cat"],\
["What noise does a truly advanced machine make?","ping"]]
if x == -1:
return lista
else: #of course, it's recommended to check if x is out of bounds
return lista[x]

menu_item = 0
while menu_item != 9:
print "------------------------------------"
print "1. Take test"
print "2. Display all questions and answers."
print "9. quit"
menu_item = input("Pick an item from the menu: ")
if menu_item == 1:
def check_question(question_and_answer):
question = question_and_answer[0]
answer = question_and_answer[1]
given_answer = raw_input(question) #what if user just press Enter?
if answer == given_answer:
print "Correct"
return true
else:
print "Incorrect, correct was: ",answer
return false
def run_test(questions):
if len(questions) == 0:
print "No questions were given."
return
index = 0
right = 0
while index < len(questions):
if check_question(questions[index]):
right = right + 1
index = index + 1
print "You got ", right*100/len(questions),"% right out of",len(questions)
run_test(get_questions())

elif menu_item == 2:
index = 0
length_quest = len(get_questions())
if length_quest > 0:
while index < length_quest:
print 'Question: ', get_questions(index)[0],
print 'Answer: ', get_questions(index)[1]
index = index + 1
else:
print "No questions"
print "Goodbye."

[/php]
Last edited by Micko; Sep 1st, 2006 at 3:02 am.
Reply With Quote Quick reply to this message  
Join Date: Sep 2006
Posts: 109
Reputation: chris99 is an unknown quantity at this point 
Solved Threads: 0
chris99's Avatar
chris99 chris99 is offline Offline
Junior Poster

Re: Non-programmer tutorial, stuck at list exercise

 
0
  #3
Sep 1st, 2006
Thank you sooo much. Now for the next section!
Reply With Quote Quick reply to this message  
Join Date: Aug 2005
Posts: 1,523
Reputation: Ene Uran has a spectacular aura about Ene Uran has a spectacular aura about 
Solved Threads: 169
Ene Uran's Avatar
Ene Uran Ene Uran is offline Offline
Posting Virtuoso

Re: Non-programmer tutorial, stuck at list exercise

 
0
  #4
Sep 1st, 2006
Just a few observations, hope you don't mind:

1) Python has True = 1 and False = 0 builtin

2) define your functions outside of the conditional if, or Python will redefine your functions whenever you take a test. This would slow down the program. The memory manager cleans up properly though.

3) use a try/except to protect against non numeric entries

Here is the code sample updated:
  1. #true = 1 # Python has True = 1
  2. #false = 0 # Python has False = 0
  3.  
  4. def get_questions(x = -1):
  5. lista = [["What colour is the daytime sky on a clear day?","blue"],
  6. ["What is the answer to life, the universe and everything?","42"],
  7. ["What is a three letter word for mouse trap?","cat"],
  8. ["What noise does a truly advanced machine make?","ping"]]
  9. if x == -1:
  10. return lista
  11. else: #of course, it's recommended to check if x is out of bounds
  12. return lista[x]
  13.  
  14. def check_question(question_and_answer):
  15. question = question_and_answer[0]
  16. answer = question_and_answer[1]
  17. given_answer = raw_input(question)
  18. if answer == given_answer:
  19. print "Correct"
  20. return True
  21. else:
  22. print "Incorrect, correct was: ",answer
  23. return False
  24.  
  25. def run_test(questions):
  26. if len(questions) == 0:
  27. print "No questions were given."
  28. return
  29. index = 0
  30. right = 0
  31. while index < len(questions):
  32. if check_question(questions[index]):
  33. right = right + 1
  34. index = index + 1
  35. print "You got ", right*100/len(questions),"% right out of",len(questions)
  36.  
  37. menu_item = 0
  38. while menu_item != 9:
  39. print "------------------------------------"
  40. print "1. Take test"
  41. print "2. Display all questions and answers."
  42. print "9. quit"
  43. try:
  44. menu_item = input("Pick an item from the menu: ")
  45. except:
  46. # protect against just enter or not numeric
  47. print '-' * 36
  48. print "Need integer input!"
  49. # pick a non existing menu_item to start at top of while loop
  50. menu_item = 3
  51.  
  52. if menu_item == 1:
  53. run_test(get_questions())
  54.  
  55. elif menu_item == 2:
  56. index = 0
  57. length_quest = len(get_questions())
  58. if length_quest > 0:
  59. while index < length_quest:
  60. print 'Question: ', get_questions(index)[0],
  61. print 'Answer: ', get_questions(index)[1]
  62. index = index + 1
  63. else:
  64. print "No questions"
  65.  
  66. print "Goodbye."
Great quiz program though, I love it! Python is a fun language to learn!
Last edited by Ene Uran; Sep 1st, 2006 at 2:17 pm.
drink her pretty
Reply With Quote Quick reply to this message  
Join Date: Sep 2006
Posts: 109
Reputation: chris99 is an unknown quantity at this point 
Solved Threads: 0
chris99's Avatar
chris99 chris99 is offline Offline
Junior Poster

Re: Non-programmer tutorial, stuck at list exercise

 
0
  #5
Sep 2nd, 2006
I need help now making the HiLo game use the last two digits of the current time. Once I've imported the time module, what then? I know how to get the time in seconds, ctime, but how do I isolate the last two numbers and make them my "Number" variable?
Reply With Quote Quick reply to this message  
Join Date: Oct 2004
Posts: 3,971
Reputation: vegaseat is just really nice vegaseat is just really nice vegaseat is just really nice vegaseat is just really nice vegaseat is just really nice 
Solved Threads: 920
Moderator
vegaseat's Avatar
vegaseat vegaseat is offline Offline
DaniWeb's Hypocrite

Re: Non-programmer tutorial, stuck at list exercise

 
0
  #6
Sep 2nd, 2006
Maybe this will help you ...
  1. import time
  2.  
  3. print time.ctime() # Sat Sep 02 18:23:53 2006
  4.  
  5. # get just hr, min, sec of present time tuple
  6. hr, min, sec = time.localtime()[3:6]
  7.  
  8. print hr, min, sec # 18 23 53
  9.  
  10. print sec, type(sec) # 53 <type 'int'>
  11.  
  12. # if you just want seconds
  13. sec = time.localtime()[5]
  14.  
  15. print sec # 53
May 'the Google' be with you!
Reply With Quote Quick reply to this message  
Join Date: Sep 2006
Posts: 109
Reputation: chris99 is an unknown quantity at this point 
Solved Threads: 0
chris99's Avatar
chris99 chris99 is offline Offline
Junior Poster

Re: Non-programmer tutorial, stuck at list exercise

 
0
  #7
Sep 3rd, 2006
Originally Posted by vegaseat View Post
Maybe this will help you ...
  1. import time
  2.  
  3. print time.ctime() # Sat Sep 02 18:23:53 2006
  4.  
  5. # get just hr, min, sec of present time tuple
  6. hr, min, sec = time.localtime()[3:6]
  7.  
  8. print hr, min, sec # 18 23 53
  9.  
  10. print sec, type(sec) # 53 <type 'int'>
  11.  
  12. # if you just want seconds
  13. sec = time.localtime()[5]
  14.  
  15. print sec # 53
Thanks, that works perfectly. I really appreciate all your help so far.
Reply With Quote Quick reply to this message  
Join Date: Sep 2006
Posts: 109
Reputation: chris99 is an unknown quantity at this point 
Solved Threads: 0
chris99's Avatar
chris99 chris99 is offline Offline
Junior Poster

Re: Non-programmer tutorial, stuck at list exercise

 
0
  #8
Sep 3rd, 2006
Problems lie now with saving and loading. I'm on to File IO and it is complex. Do I just need in the def save_student() bracket (student{}) Or will I need more than just the library? Or more specific? I'm trying to save the grade + student data. Here is the code:

  1. max_points = [25,25,50,25,100]
  2. assignments = ["hw ch 1","hw ch 2","quiz ","hw ch 3","test"]
  3. students = {"#Max":max_points}
  4. def print_menu():
  5. print "1. Add student"
  6. print "2. Remove student"
  7. print "3. Print grades"
  8. print "4. Record grade"
  9. print "5. Exit"
  10. def print_all_grades():
  11. print "\t",
  12. for i in range(len(assignments)):
  13. print assignments[i],"\t",
  14. print
  15. keys = students.keys()
  16. keys.sort()
  17. for x in keys:
  18. print x,"\t",
  19. grades = students[x]
  20. print_grades(grades)
  21.  
  22. def print_grades(grades):
  23. for i in range(len(grades)):
  24. print grades[i],"\t\t",
  25. print
  26. print_menu()
  27. menu_choice = 0
  28. while menu_choice != 5:
  29. print
  30. menu_choice = input("Menu Choice (1-6): ")
  31. if menu_choice == 1:
  32. name = raw_input("Student to add: ")
  33. students[name] = [0]*len(max_points)
  34. elif menu_choice == 2:
  35. name = raw_input("Stuent to remove: ")
  36. if students.has_key(name):
  37. del students[name]
  38. else:
  39. print "Student: ",name," not found"
  40. elif menu_choice == 3:
  41. print_all_grades()
  42. elif menu_choice == 4:
  43. print "Record Grade"
  44. name = raw_input("Student: ")
  45. if students.has_key(name):
  46. grades = students[name]
  47. print "Type the number of the grade to record"
  48. print "Type a 0 (zero) to exit"
  49. for i in range(len(assignments)):
  50. print i+1," ",assignments[i],"\t",
  51. print
  52. print_grades(grades)
  53. which = 1234
  54. while which != -1:
  55. which = input("Change which Grade")
  56. which = which-1
  57. if 0 <= which < len(grades):
  58. grade = input("Grade: ")
  59. grades[which] = grade
  60. elif which != -1:
  61. print "Invalid Grade Number"
  62. else:
  63. print "Student not found"
  64. elif menu_choice != 5:
  65. print_menu()

Here is the tutorial task:

Now modify the grades program from section 11 so that is uses file IO to keep a record of the students.
Last edited by chris99; Sep 3rd, 2006 at 8:12 am. Reason: misspelling
Reply With Quote Quick reply to this message  
Join Date: Sep 2006
Posts: 109
Reputation: chris99 is an unknown quantity at this point 
Solved Threads: 0
chris99's Avatar
chris99 chris99 is offline Offline
Junior Poster

Re: Non-programmer tutorial, stuck at list exercise

 
0
  #9
Sep 4th, 2006
Please can someone help me? I've given you all the info I can, and this should be the last hurdle.
Reply With Quote Quick reply to this message  
Join Date: Aug 2005
Posts: 1,523
Reputation: Ene Uran has a spectacular aura about Ene Uran has a spectacular aura about 
Solved Threads: 169
Ene Uran's Avatar
Ene Uran Ene Uran is offline Offline
Posting Virtuoso

Re: Non-programmer tutorial, stuck at list exercise

 
0
  #10
Sep 4th, 2006
Since you want to save and later load an entire container object, the module pickle is what you want to use. Hre is a simple example:
  1. # pickle saves and loads any Python object intact
  2. # via a byte stream, use cpickle for higher speed
  3.  
  4. import pickle
  5.  
  6. myList1 = [12, 52, 62, 02, 3.140, "Monte"]
  7. print "Original list:"
  8. print myList1
  9. file = open("list1.dat", "w")
  10. pickle.dump(myList1, file)
  11. file.close()
  12. file = open("list1.dat", "r")
  13. myList2 = pickle.load(file)
  14. file.close()
  15. print "List after pickle.dump() and pickle.load():"
  16. print myList2
In your case you would pickle the students dictionary!
Last edited by Ene Uran; Sep 4th, 2006 at 3:59 pm.
drink her pretty
Reply With Quote Quick reply to this message  
Reply

This thread is more than three months old.
Perhaps start a new thread instead?
Message:



Other Threads in the Python Forum
Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC