943,964 Members | Top Members by Rank

Ad:
  • Python Discussion Thread
  • Unsolved
  • Views: 3097
  • Python RSS
You are currently viewing page 1 of this multi-page discussion thread
Sep 1st, 2006
0

Non-programmer tutorial, stuck at list exercise

Expand Post »
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:

Python Syntax (Toggle Plain Text)
  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:

Quote ...
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?
Reputation Points: 10
Solved Threads: 0
Junior Poster
chris99 is offline Offline
118 posts
since Sep 2006
Sep 1st, 2006
0

Re: Non-programmer tutorial, stuck at list exercise

Well, you have defined function get_questions() not to take any arguments, but in lines:
Python Syntax (Toggle Plain Text)
  1. while current < len(get_questions(index)):
and
Python Syntax (Toggle Plain Text)
  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.
Reputation Points: 55
Solved Threads: 6
Junior Poster
Micko is offline Offline
148 posts
since Aug 2005
Sep 1st, 2006
0

Re: Non-programmer tutorial, stuck at list exercise

Thank you sooo much. Now for the next section!
Reputation Points: 10
Solved Threads: 0
Junior Poster
chris99 is offline Offline
118 posts
since Sep 2006
Sep 1st, 2006
0

Re: Non-programmer tutorial, stuck at list exercise

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:
Python Syntax (Toggle Plain Text)
  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.
Reputation Points: 625
Solved Threads: 211
Posting Virtuoso
Ene Uran is offline Offline
1,704 posts
since Aug 2005
Sep 2nd, 2006
0

Re: Non-programmer tutorial, stuck at list exercise

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?
Reputation Points: 10
Solved Threads: 0
Junior Poster
chris99 is offline Offline
118 posts
since Sep 2006
Sep 2nd, 2006
0

Re: Non-programmer tutorial, stuck at list exercise

Maybe this will help you ...
Python Syntax (Toggle Plain Text)
  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
Moderator
Reputation Points: 1333
Solved Threads: 1403
DaniWeb's Hypocrite
vegaseat is offline Offline
5,792 posts
since Oct 2004
Sep 3rd, 2006
0

Re: Non-programmer tutorial, stuck at list exercise

Click to Expand / Collapse  Quote originally posted by vegaseat ...
Maybe this will help you ...
Python Syntax (Toggle Plain Text)
  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.
Reputation Points: 10
Solved Threads: 0
Junior Poster
chris99 is offline Offline
118 posts
since Sep 2006
Sep 3rd, 2006
0

Re: Non-programmer tutorial, stuck at list exercise

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:

Python Syntax (Toggle Plain Text)
  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:

Quote ...
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
Reputation Points: 10
Solved Threads: 0
Junior Poster
chris99 is offline Offline
118 posts
since Sep 2006
Sep 4th, 2006
0

Re: Non-programmer tutorial, stuck at list exercise

Please can someone help me? I've given you all the info I can, and this should be the last hurdle.
Reputation Points: 10
Solved Threads: 0
Junior Poster
chris99 is offline Offline
118 posts
since Sep 2006
Sep 4th, 2006
0

Re: Non-programmer tutorial, stuck at list exercise

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:
Python Syntax (Toggle Plain Text)
  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.
Reputation Points: 625
Solved Threads: 211
Posting Virtuoso
Ene Uran is offline Offline
1,704 posts
since Aug 2005

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in Python Forum Timeline: Trying to save the info in the student library.
Next Thread in Python Forum Timeline: Graphics and Game commands:





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC