Advertisement
graemeblake

CS 101 final project

Apr 20th, 2012
101
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 30.12 KB | None | 0 0
  1. #This project has a creative commons CC BY-NC-SA license.
  2. #The author is Graeme Blake. Contact graemeblake at gmail if you have any questions
  3.  
  4.  
  5. ##################################################################
  6.  
  7.                              #OUTLINE:
  8.                              
  9. #This program almost certainly violates best practices.
  10.  
  11. #But it works.
  12.  
  13. #Its a series of function calls. The functions pass control amongst themseleves.
  14. #The program goes deeper and deeper, and there are many infinite loops.
  15. #This doesn't matter, as the program's needs aren't great.
  16.  
  17. #A global variable tracks input, and the student score on various exercies.
  18.  
  19. #There is an option to input a score code to return to a previous session.
  20.  
  21. #The program is quite extensible. New modules can be added by modifying the following things:
  22.  
  23. #*The list of topics, and the dictionary of points.
  24. #*The help function, if necessary. Also add text for new help topics.
  25. #*The global commands function, for new topics.
  26.  
  27. #That's it, unless a new feature is added.
  28.  
  29. #The func() function is quite important. It's called whenever the user
  30. #has entered input. If necessary, it redirects to the appropriate function.
  31. #Otherwise it does nothing, and the current function continues.
  32.  
  33.  
  34. ##################################################################
  35.  
  36.                              #LIBRARIES
  37.  
  38. ##################################################################
  39.  
  40. import math #I don't think I need this
  41. import random
  42.  
  43.  
  44. ##################################################################
  45.  
  46.                              #GLOBAL VARIABLES
  47.  
  48. ##################################################################
  49.  
  50. topic_list = ["assignment", "arrays"] #This is the list of exercise topics
  51. points = {"assignment1":0, "assignment2": 0, "arrays1": 0, "arrays2": 0, "arrays3":0} #This tracks a student's score for each topic
  52. input = "" #Global variable to track last student response
  53. global max_score
  54. max = 9
  55.  
  56.  
  57.  
  58. ##################################################################
  59.  
  60.                         #CONTROL FLOW FUNCTIONS
  61.                        
  62. ##################################################################
  63.  
  64. #INPUT2#
  65.  
  66. #This is a global variable to get user input
  67. def input2():
  68.     global input
  69.     input = raw_input(">> ")
  70.     return
  71.  
  72.  
  73. ##################################################################
  74.  
  75. #PRINT_SCORE#
  76.  
  77. #This prints out a score code for the user.  
  78. def print_score():
  79.     print "\nSCORE CODE"
  80.     print "\nHere is your score code.\nWrite it down to continue your session later:\n"
  81.     score = ''
  82.     for e in points:
  83.         score += "%d" % points[e]
  84.     print "\nYour score code is: %s" % score
  85.  
  86. #This lets a user enter a score code which
  87. #sets the values of the points dictionary
  88.  
  89. ##################################################################
  90.  
  91. #ENTER_CODE#
  92.  
  93. def enter_code():
  94.     print "\nSCORE CODE ENTRY"
  95.     print "\nPlease enter your score code\n"
  96.     length = len(points)
  97.     while 1:
  98.         input2()
  99.         func()
  100.         try:
  101.             if eval('1' + input) >= 0 and len(input) == length: #The '1' corrects for an error
  102.                 i = 0
  103.                 for e in points:
  104.                     points[e] = eval(input[i])
  105.                     i += 1
  106.                 print "\nSuccess! Your code worked."
  107.                 break
  108.             else:
  109.                 print "\nplease enter a real score code.\n\nThe score code should be a positive whole number, %d digits long" % length        
  110.         except:
  111.             print "\nplease enter a real score code.\n\nThe score code should be a positive whole number, %d digits long" % length
  112.  
  113. ##################################################################
  114.  
  115. #MAIN#
  116.  
  117. #This tells the user where they are, and asks them to choose a topic.
  118. def main():
  119.     print """
  120. MAIN MENU    
  121.  
  122. You're at the main menu.
  123. Here is the list of topics:\n
  124. %s
  125.  
  126. Type the topic name for exercies, or type help for more info.\n""" % topic_list
  127.     while 1:
  128.         input2()
  129.         func()
  130.         if input == "arrays":
  131.             arrays()
  132.         elif input == "assignment":
  133.             assignment()
  134.         else:
  135.             print """
  136. Sorry, I'm just a dumb computer, I don't understand typos.
  137. Capital letters are also difficult.
  138.  
  139. Type "help" for help.
  140. Type "main" to go to the  main menu.
  141. """
  142.     return
  143.  
  144. ##################################################################
  145.  
  146. #HELP#
  147.  
  148. #This enters the help menu for topics. The func commands lets the user escape.
  149. def help():
  150.     print """
  151. HELP MENU    
  152.  
  153. Type any of the following commands to learn more about a topic.
  154. Type main to return to the main menu.
  155.        
  156. \n\nassignment
  157. \narrays
  158. \ntypos
  159. \npoints
  160. \nsaving progress
  161. """
  162.     while 1:
  163.         input2()
  164.         func()
  165.         if input == "assignment":
  166.             assignment_help()
  167.         elif input == "arrays":
  168.             arrays_help()
  169.         elif input == "typos":
  170.             typos_help()
  171.         elif input == "points":
  172.             points_help()
  173.         elif input == "saving progress":
  174.             saving_progress_help()
  175.         else:
  176.             print """
  177. I didn't recognize that input.
  178.  
  179. Type a topic name for information about that topic.
  180. Type "help" for the list of topics.
  181. Type "main" to return to the main menu.
  182. """
  183.  
  184. ##################################################################
  185.  
  186. #FUNC#
  187.  
  188. #At each interval, this checks if the user wanted to go to one of the other menus.
  189. #If not, the function returns control.
  190. def func():
  191.     if input == "help":
  192.         help()
  193.     elif input == "main":
  194.         main()
  195.     elif input == "score":
  196.         print_score()
  197.         main()
  198.     elif input == "code":
  199.         enter_code()
  200.         main()
  201.     return
  202.    
  203.    
  204.  
  205. ##################################################################
  206.  
  207.                             #HELP FUNCTIONS
  208.                            
  209. #This is the text for the information available in the help functions
  210.  
  211. ##################################################################
  212.  
  213. #ASSIGNMENT_HELP#
  214.  
  215. def assignment_help():
  216.     print """
  217. ASSIGNMENT HELP
  218.  
  219. Assignment gives a value to a variable. For example:
  220. \nx = 4
  221. \nThe line above sets the value of x equal to 4. You can change the value of x.
  222.  
  223. Let's try something more complicated. Write these on paper if you're unsure; it helps.
  224. \nx = 37
  225. x = x + 3
  226. x += 2
  227. \nx now equals 42.
  228.  
  229. First, we set x to 37.
  230. Then we set x equal to the value of x (37) + 3, which is 40.
  231. += means we take the value of the variable, and add the number on the right.
  232. x was 40, so now we add 2 to x.
  233.  
  234. x += 2  and x = x + 2 are the same operation. Use whichever you prefer.
  235.  
  236. You can do all sorts of fancy math with assignment. For instance:
  237.  
  238. x = 4
  239. y = 7
  240. z = x * y
  241.  
  242. z now equals 28.
  243.  
  244. Note that Z does not exist. Capital letters matter in variable names.
  245. """
  246. ##################################################################
  247.  
  248. #ARRAYS_HELP#
  249.  
  250. def arrays_help():
  251.     print """
  252. ARRAYS HELP
  253.  
  254. A list (also called an array) holds a series of values. For example:
  255.  
  256. numbers = [1, 2, 3, 4, 5]
  257.  
  258. You access list locations by using square brackets
  259. and a number for the location.
  260.  
  261. Confusingly, lists start from the number zero.
  262. So numbers[0] = 1.
  263. numbers[4] = 5
  264. numbers[5] doesn't exist.
  265.  
  266. We could expand our list. For example, we can add a number
  267. using the append function.
  268.  
  269. numbers.append(54) -->
  270. print numbers
  271. [1, 2, 3, 4, 5, 54]
  272.  
  273. You can measure the length of a list with the len() function.
  274.  
  275. print len(numbers)
  276. 6
  277.  
  278. You can also select subsections of lists. For example:
  279.  
  280. print numbers [2:4]
  281. [3, 4]
  282.  
  283. Think of it as starting from 2, and going all the way up to the edge of 4,
  284. but not actually including number[4]. So only numbers[2] and numbers[3]
  285. are selected.
  286.  
  287. Lists can include strings too. In fact, you can mix numbers and strings.
  288.  
  289. mix = ["Udacity", "is", 2938, "?"]
  290.  
  291. That's a legal list, though it's certainly odd.
  292.  
  293. Lastly, lists can be multi-dimensional. This list has three objects:
  294.  
  295. list = [1, [2,3,5], 70]
  296.  
  297. The second object is a list. So:
  298. list[0] = 1
  299. list[2] = 70
  300. list[1] = [2,3,5]
  301.  
  302. What if you want to access 5? Enter the position of five in the list:
  303.  
  304. list[1][2] = 5
  305. """
  306.     return
  307.    
  308. ##################################################################
  309.  
  310. #POINTS_HELP#
  311.  
  312. def points_help():
  313.     print """
  314. POINTS HELP
  315.  
  316. Your points are stored in a simple dictionary. Each topic has its own key.
  317.  
  318. If you get a question right, you'll get a point for that topic.
  319. Get a question wrong, and your points get cut in half.
  320. If the computer doesn't understand your answer, you lose a point.
  321.  
  322. Get nine points in a topic, and you've mastered it.
  323.  
  324. You can get a score code for your points. Type "score" at any time.
  325.  
  326. When you restart a session, type "code" to re-enter your score code.
  327.  
  328. It would be very easy to game this system, but why bother?
  329. """
  330.     return
  331.    
  332. ##################################################################
  333.  
  334. #SAVING_PROGRESS#
  335.  
  336. def saving_progress_help():
  337.     print """"
  338. SAVING PROGRESS HELP
  339.  
  340. At any time, type "score" to get a score code.
  341. When you restart, you can type "code" to enter your code and resume
  342. your session.
  343.  
  344. Both the "score" and "code" commands return you to the main menu afterwards.
  345. """
  346.     return
  347.    
  348. ##################################################################
  349.  
  350. #TYPOS_HELP#
  351.  
  352. def typos_help():
  353.     print """
  354. TYPOS HELP
  355.  
  356. I've tried to program for all types of input. But I don't always know how.
  357.  
  358. So try and type things in a normal way.
  359.  
  360. For example:
  361.  
  362. list.append(42)
  363. list.append( 42 )
  364. list.append(             42    )
  365.  
  366. Those are all legal ways to append 42 to list. But I've only coded the
  367. program to accept the first too.
  368.  
  369. Likewise, I usually don't accept:
  370.  
  371. a =             6
  372.  
  373. Even though that is a way to set a to 6. Some exercises may accept this,
  374. but don't count on it.
  375. """
  376.     return
  377.  
  378.  
  379. ##################################################################
  380.  
  381.                              #EXERCISE FUNCTIONS
  382.  
  383. ##################################################################
  384.  
  385.  
  386. #Functions are below, by Category
  387.  
  388.  
  389.  
  390.  
  391.  
  392.  
  393.  
  394. ##################################################################
  395.  
  396.                              #ARRAY FUNCTIONS
  397.  
  398. ##################################################################
  399.  
  400. #ARRAYS#
  401.  
  402. #This just directs the user to choose the level of difficulty
  403.  
  404. def arrays():
  405.     print """
  406. Would you like to do array exercises level 1, 2 or 3?
  407.  
  408. Enter 1, 2 or 3.
  409. """
  410.     while 1:
  411.         input2()
  412.         func()
  413.         if input == "1" or input == "arrays1":
  414.             arrays1()
  415.         elif input == "2" or input == "arrays2":
  416.             arrays2()
  417.         elif input == "3" or input == "arrays3":
  418.             arrays3()
  419.         else:
  420.             print """
  421. Oops. Please enter 1, 2 or 3. Or type "main" to go back to the main menu.\
  422. \nType "help" for a list of commands.
  423. """
  424.  
  425.  
  426. ##################################################################
  427.  
  428. #ARRAYS LEVEL 1#
  429.  
  430. #This tests whether the user knows how to access elements in an array#
  431.  
  432. def arrays1():
  433.     print """
  434. ARRAYS ONE INTRO
  435.  
  436. THis will teach you how to access locations in an array.
  437. """
  438.     while 1:
  439.         list = []
  440.         length = random.randint(3,12)
  441.         for i in range(0, length):
  442.             list.append(random.randint(0, 28))
  443.         loc = random.randint(0, length - 1)
  444.         location = "list[%d]" % loc
  445.         print "\nQUESTION:"
  446.         print "\nhere is a list of objects called \"list\":"
  447.         print "\nlist = %s    (length of list = %d)" % (list, length)
  448.         print "which list location has this value: %d" % eval(location)
  449.         print "\nenter how you would access that location. \nThere may be more than one possibility.\n"
  450.        
  451.         if points["arrays1"] >= max:
  452.             print "\nYou've mastered this exercise.\nType \"main\" to try something else.\n\n(Feel free to keep going, there's another question above.)"
  453.         while 1:
  454.             input2()
  455.             func()
  456.             try:
  457.                 if input == "new":
  458.                     break
  459.                 elif eval(input) == list[loc]:
  460.                     print "\n\nCORRECT"
  461.                     if points["arrays1"] < 9:
  462.                         points["arrays1"] += 1
  463.                     if points["arrays1"] != 1:
  464.                         print "You have %d points!" % points["arrays1"]
  465.                     elif points["arrays1"] == 1:
  466.                         print "You have %d point!" % points["arrays1"]
  467.                     break
  468.                 else:
  469.                     points["arrays1"] /= 2
  470.                     print "\noops, that's not right."
  471.                     if points["arrays1"] != 1:
  472.                         print "You have %d points!" % points["arrays1"]
  473.                     elif points["arrays1"] == 1:
  474.                         print "You have %d point!" % points["arrays1"]
  475.                     print "\n\nlist = %s\n%s equals %d.\
  476.                    \nYou were supposed to find which location has the value %d."\
  477.                     % (list, input, eval(input), list[loc])
  478.                     print "\nTry again, or type \"new\" for another question"
  479.             except:
  480.                 if points["arrays1"] > 0:
  481.                     points["arrays1"] -= 1
  482.                 if points["arrays1"] != 1:
  483.                     print "You have %d points!" % points["arrays1"]
  484.                 elif points["arrays1"] == 1:
  485.                     print "You have %d point!" % points["arrays1"]
  486.                 print "I didn't recognize that input."
  487.                 print "\nTry again, or type \"new\" for another question"
  488.        
  489. ##################################################################
  490.  
  491. #ARRAYS LEVEL 2#        
  492.    
  493. def arrays2():
  494.     print """
  495. ARRAYS TWO INTRO
  496.  
  497. This will teach you a few important functions for using arrays.
  498. """
  499.     while 1:
  500.         type = random.randint(1,5)
  501.         if type == 1: #length function
  502.             print "\n QUESTION:"
  503.             print "\nHow do you measure the length of this list?"
  504.             list = []
  505.             length = random.randint(3,12)
  506.             for i in range(0, length):
  507.                 list.append(random.randint(0, 28))
  508.             print "list = %s" % list
  509.             while 1:
  510.                 if points["arrays2"] >= max:
  511.                     print "\nYou've mastered this exercise.\nType \"main\" to try something else.\n\n(Feel free to keep going, there's another question above.)"
  512.                 input2()
  513.                 func()
  514.                 try:
  515.                     if input == "new":
  516.                         break
  517.                     elif eval(input) == length and input != str(length): #disallow simply typing length
  518.                         print "\n\nCORRECT"
  519.                         if points["arrays2"] < 9:
  520.                             points["arrays2"] += 1
  521.                         if points["arrays2"] != 1:
  522.                             print "You have %d points!" % points["arrays2"]
  523.                         elif points["arrays2"] == 1:
  524.                             print "You have %d point!" % points["arrays2"]
  525.                         break
  526.                     else:
  527.                         print "\noops, that's not right.\n\nUse the len() function\
  528.                        \ne.g. len(list) --> %d" % length
  529.                         points["arrays2"] /= 2
  530.                         break
  531.                 except:
  532.                     print "\noops, that's not right.\n\nUse the len() function\
  533.                    \ne.g. len(list) --> %d" % length
  534.                     if points["arrays1"] > 0:
  535.                         points["arrays1"] -= 1
  536.                     break
  537.         if type == 2:  #sub-strings
  538.             print "\n QUESTION:"
  539.             print "\nHow do you select the string \"Udacity\" from this array?"
  540.             list = []
  541.             length = random.randint(1,4)
  542.             for i in range(0, length):
  543.                 list.append(random.randint(0, 28))
  544.             for i in "Udacity":
  545.                 list += i
  546.             length = random.randint(1,4)
  547.             for i in range(0, length):
  548.                 list.append(random.randint(0, 28))
  549.             print "list = %s" % list
  550.             while 1:
  551.                 if points["arrays2"] >= max:
  552.                     print "\nYou've mastered this exercise.\nType \"main\" to try something else.\n\n(Feel free to keep going, there's another question above.)"
  553.                 input2()
  554.                 func()
  555.                 try:
  556.                     if input == "new":
  557.                         break
  558.                     elif eval(input) == ['U', 'd', 'a', 'c', 'i', 't', 'y']:
  559.                         print "\n\nCORRECT\n"
  560.                         if points["arrays2"] < 9:
  561.                             points["arrays2"] += 1
  562.                         if points["arrays2"] != 1:
  563.                             print "You have %d points!" % points["arrays2"]
  564.                         elif points["arrays2"] == 1:
  565.                             print "You have %d point!" % points["arrays2"]
  566.                        
  567.                         break
  568.                     else:
  569.                         print "\noops, that's not right.\n\nHere is what you selected: %s" % eval(input)
  570.                         print "\nTry again, or type \"new\" for another question"
  571.                         points["arrays2"] /= 2
  572.                 except:
  573.                     print "I didn't recognize that input."
  574.                     print "\nTry again, or type \"new\" for another question"
  575.                     if points["arrays2"] > 0:
  576.                         points["arrays2"] -= 1
  577.                     break
  578.         if type == 3: #appending
  579.             print "\n QUESTION:"
  580.             print "\nHow do you modify this list to add 42 at the end?"
  581.             list = []
  582.             length = random.randint(3,12)
  583.             for i in range(0, length):
  584.                 list.append(random.randint(0, 28))
  585.             print "list = %s" % list
  586.             while 1:
  587.                 if points["arrays2"] >= max:
  588.                     print "\nYou've mastered this exercise.\nType \"main\" to try something else.\n\n(Feel free to keep going, there's another question above.)"
  589.                 input2()
  590.                 func()
  591.                 try:
  592.                     if input == "new":
  593.                         break
  594.                     elif input == "list.append(42)" or input == "list.append( 42)" or input == "list.append(42 )" or input == "list.append( 42 )":
  595.                         print "\n\nCORRECT"
  596.                         if points["arrays2"] < 9:
  597.                             points["arrays2"] += 1
  598.                         if points["arrays2"] != 1:
  599.                             print "You have %d points!" % points["arrays2"]
  600.                         elif points["arrays2"] == 1:
  601.                             print "You have %d point!" % points["arrays2"]
  602.                         break
  603.                     else:
  604.                         print "\noops, that's not right.\n\nUse the append() function\
  605.                        \ne.g. list.append(42)"
  606.                         points["arrays2"] /= 2
  607.                         break
  608.                 except:
  609.                     print "I didn't understand that input"
  610.                     print "\nTry again, or type \"new\" for another question"
  611.                     if points["arrays2"] > 0:
  612.                             points["arrays2"] -= 1
  613.    
  614. ##################################################################
  615.  
  616. #ARRAYS LEVEL #3
  617.  
  618. def arrays3():    #multi-dimensional arrays
  619.     print """
  620. ARRAYS 3 INTRO
  621.  
  622. This will teach you multi-dimensional arrays.
  623. """
  624.     while 1:
  625.         list = []
  626.         length = random.randint(3,9)
  627.         for i in range(0, length):
  628.             list.append([])
  629.             for e in range(random.randint(1,4)):
  630.                 list[i].append(random.randint(0, 28))
  631.         loc1 = random.randint(0, length - 1)
  632.         loc2 = random.randint(0, len(list[loc1]) - 1)
  633.         location = "list[%d][%d]" % (loc1, loc2)
  634.         print "\n QUESTION:"
  635.         print "\nhere is a list of objects called \"list\":"
  636.         print "\nlist = %s    \n(length of list = %d)" % (list, length)
  637.         print "which list location has this value: %d" % eval(location)
  638.         print "\nenter how you would access that location. \nThere may be more than one possibility.\n"
  639.        
  640.         if points["arrays3"] >= max:
  641.             print "\nYou've mastered this exercise.\nType \"main\" to try something else.\n\n(Feel free to keep going, there's another question above.)"
  642.         while 1:
  643.             input2()
  644.             func()
  645.             try:
  646.                 if input == "new":
  647.                     break
  648.                 elif input != '%s' % str(list[loc1][loc2]) and eval(input) == list[loc1][loc2]:
  649.                     print "\n\nCORRECT"
  650.                     if points["arrays3"] < 9:
  651.                         points["arrays3"] += 1
  652.                     if points["arrays3"] != 1:
  653.                         print "You have %d points!" % points["arrays3"]
  654.                     elif points["arrays3"] == 1:
  655.                         print "You have %d point!" % points["arrays3"]
  656.                     break
  657.                 else:
  658.                     print "\noops, that's not right.\n\nlist = %s\n%s equals %d.\
  659.                    \nYou were supposed to find which location has the value %d."\
  660.                     % (list, input, eval(input), list[loc1][loc2])
  661.                     print "\nTry again, or type \"new\" for another question"
  662.                     points["arrays3"] /= 2
  663.             except:
  664.                 print "I didn't recognize that input."
  665.                 print "\nTry again, or type \"new\" for another question"
  666.                 if points["arrays3"] > 0:
  667.                     points["arrays3"] -= 1
  668.    
  669.  
  670. ##################################################################
  671.  
  672.                              #ASSIGNMENT FUNCTIONS
  673.  
  674. ##################################################################
  675.  
  676. def assignment():
  677.     print """
  678. Would you like to do assignment exercises level 1 or 2?
  679.  
  680. Enter 1 or 2.
  681. """
  682.     while 1:
  683.         input2()
  684.         func()
  685.         if input == "1" or input == "assignment1":
  686.             assignment1()
  687.         elif input == "2" or input == "assignment2":
  688.             assignment2()
  689.         else:
  690.             print """
  691. Oops. Please enter 1 or 2. Or type "main" to go back to the main menu.\
  692. \nType "help" for a list of commands.
  693. """
  694.     return
  695.    
  696. ##################################################################
  697.  
  698.                              #ASSIGNMENT One
  699.  
  700. ##################################################################
  701.  
  702. #This tests very basic assignment concepts.
  703. def assignment1():
  704.     print """
  705. ASSIGNMENT ONE INTRO
  706.  
  707. This will teach you the basic assignment operators.
  708. You'll also learn multiple assignment.
  709. """
  710.  
  711.     while 1:
  712.         a = random.randint(0,15)
  713.         b = random.randint(0,15)
  714.         c = random.randint(0,15)
  715.         d = random.randint(0,15)
  716.         e = random.randint(0,15)
  717.         array = ["a", "b", "c", "d", "e"]
  718.    
  719.         num1 = array[random.randint(1, len(array) - 1)]
  720.         num2 = array[random.randint(1, len(array) - 1)]
  721.         num3 = array[random.randint(1, len(array) - 1)]
  722.         num4 = array[random.randint(1, len(array) - 1)]
  723.         num5 = array[random.randint(1, len(array) - 1)]
  724.         num6 = array[random.randint(1, len(array) - 1)]
  725.         num7 = array[random.randint(1, len(array) - 1)]
  726.         num8 = array[random.randint(1, len(array) - 1)]
  727.    
  728.         question = random.randint(1,4)
  729.         if question == 1: #basic assignment
  730.             print """
  731. QUESTION:
  732.  
  733. a = %d\n
  734. b = %d
  735. c = %d
  736. d = %d
  737. e = %d
  738. """ %(a, b, c, d, e)
  739.             print "How would you set a equal to %s" % num1
  740.             a = eval(num1)
  741.             if points["assignment1"] >= max:
  742.                 print "\nYou've mastered this exercise.\nType \"main\" to try something else.\n\n(Feel free to keep going, there's another question above.)"
  743.             input2()
  744.             func()
  745.             if input == "a = %s" %num1 or input == "a = %d" % eval(num1):
  746.                 print "CORRECT"
  747.                 points["assignment1"] += 1
  748.                 if points["assignment1"] != 1:
  749.                     print "You have %d points!" % points["assignment1"]
  750.                 elif points["assignment1"] == 1:
  751.                     print "You have %d point!" % points["assignment1"]
  752.             else:
  753.                 print "Sorry, that's not right. Make sure it's not a typo.\
  754.                You should have typed \"a = %s" % num1
  755.                 points["assignment1"] /= 2
  756.         if question == 2: #multiple assignment
  757.             copy = []
  758.             copy += array
  759.             print """
  760. QUESTION:
  761.  
  762. a = %d\n
  763. b = %d
  764. c = %d
  765. d = %d
  766. e = %d
  767. """ %(a, b, c, d, e)
  768.             print """
  769. a, %s = %s, %s
  770.  
  771. What is the value of a?
  772. """ % (num1, num2, num3)
  773.             a = eval(num2)
  774.             if points["assignment1"] >= max:
  775.                 print "\nYou've mastered this exercise.\nType \"main\" to try something else.\n\n(Feel free to keep going, there's another question above.)"
  776.             input2()
  777.             func()
  778.             try:
  779.                 if input == "a = %s" %num2 or input == "a = %d" %eval(num2) or eval(input) == a:
  780.                     print "CORRECT"
  781.                     points["assignment1"] += 1
  782.                     if points["assignment1"] != 1:
  783.                         print "You have %d points!" % points["assignment1"]
  784.                     elif points["assignment1"] == 1:
  785.                         print "You have %d point!" % points["assignment1"]
  786.                 else:
  787.                     print "Sorry, that's not right. Make sure it's not a typo.\
  788.                    \"a = %d" % num2
  789.                     points["assignment1"] /= 2
  790.             except:
  791.                 print "Sorry, didn't understand that."
  792.                 if points["assignment1"] > 0:
  793.                     points["assignment1"] -= 1
  794.            
  795. ##################################################################
  796.  
  797.                              #ASSIGNMENT TWO
  798.  
  799. ##################################################################
  800.  
  801. #This tests whether students can visualize changes in variables through
  802. #assignment.
  803.  
  804. #This should probably be module three, with a second module covering
  805. #the arithmetic operations.
  806.  
  807. def assignment2():
  808.     print """
  809. ASSIGNMENT TWO INTRO
  810.  
  811. This tests how well you understand assignment and it's operations.
  812. You'll see a list of variables, and a list of operations.
  813. Find the final value of a.
  814. """
  815.     while 1:
  816.         a = random.randint(0,15)
  817.         b = random.randint(0,15)
  818.         c = random.randint(0,15)
  819.         d = random.randint(0,15)
  820.         e = random.randint(0,15)
  821.         array = ["a", "b", "c", "d", "e"]
  822.    
  823.         num1 = array[random.randint(1, len(array) - 1)]
  824.         num2 = array[random.randint(1, len(array) - 1)]
  825.         num3 = array[random.randint(1, len(array) - 1)]
  826.         num4 = array[random.randint(1, len(array) - 1)]
  827.    
  828.         print """
  829. QUESTION:
  830.  
  831. What is the value of a?:\n
  832. a = %d\n
  833. b = %d
  834. c = %d
  835. d = %d
  836. e = %d
  837. """ %(a, b, c, d, e)
  838.         question = random.randint(1,4)
  839.         if question == 1:
  840.             print """
  841. a = %s + %s
  842. a += %s
  843. b *= %s
  844. a -= 5
  845. """ % (num1, num2, num3, num4)
  846.    
  847.             a = eval(num1) + eval(num2)
  848.             a += eval(num3)
  849.             a -= 5
  850.         elif question == 2:
  851.             print """
  852. a = %s - %s
  853. a *= 3
  854. q = %s * 74
  855. a = %s - 7
  856. """ % (num1, num2, num3, num4)
  857.    
  858.             a = eval(num4) - 7
  859.         elif question == 3:
  860.             print """
  861. r = %s + %s
  862. a += %s
  863. b *= %s
  864. a = a + (r * 0) + 21
  865. """ % (num1, num2, num3, num4)
  866.    
  867.             a += eval(num3)
  868.             a += 21
  869.         elif question == 4:
  870.             print """
  871. a -= a
  872. a *= %s
  873. a -= %s
  874. a *= -3
  875. """ % (num1, num2)
  876.    
  877.             a = 0
  878.             a -= eval(num2)
  879.             a *= -3
  880.         if points["assignment2"] >= max:
  881.             print "\nYou've mastered this exercise.\nType \"main\" to try something else.\n\n(Feel free to keep going, there's another question above.)"
  882.         input2()
  883.         func()
  884.         if input == "a = %d" % a and input != 'a':
  885.                 print "CORRECT"
  886.                 points["assignment2"] += 1
  887.                 if points["assignment2"] != 1:
  888.                     print "You have %d points!" % points["assignment2"]
  889.                 elif points["assignment2"] == 1:
  890.                     print "You have %d point!" % points["assignment2"]
  891.         elif input == "%d" % a:
  892.                 print "CORRECT"
  893.                 points["assignment2"] += 1
  894.                 if points["assignment2"] != 1:
  895.                     print "You have %d points!" % points["assignment2"]
  896.                 elif points["assignment2"] == 1:
  897.                     print "You have %d point!" % points["assignment2"]
  898.         else:
  899.             print "Sorry, that's not right. Make sure it's not a typo.\na = %d" % a
  900.             points["assignment2"] /= 2          
  901.            
  902. ##################################################################
  903.  
  904.                              #START OF PROGRAM
  905.                            
  906. #Just prints an
  907.  
  908. #intro and calls the main function
  909.  
  910. #The user must press enter, so they have a chance to notice the intro text.
  911.  
  912. ##################################################################
  913.  
  914. print """
  915. INTRO
  916.  
  917. Welcome to CS 101 python practice. This program is meant to help you
  918. learn programming by repeating the basic concepts.
  919. \nYou can go through a series of exercises. As you get questions right,
  920. the computer will let you know when it's time to move on.
  921.  
  922. INSTRUCTIONS
  923.  
  924. \nChoose an exercise by typing in its name.
  925. If you want to know more, type help for a list of topics.
  926. If you have a progess code to restart your session, type code to enter it.
  927. If you want to get a score code at any time type \"score\".
  928.  
  929. However, this will cause you to leave your current exercise
  930. and return to the main menu afterwards.
  931.  
  932. SCROLL UP TO SEE THE INTRO
  933.  
  934. You can type "main", "help", "score" and "code"
  935. to access those functions AT ANY TIME.
  936.  
  937. Type anything to start.
  938.  
  939. """
  940. raw_input()
  941. while 1:
  942.     main()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement