Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #This project has a creative commons CC BY-NC-SA license.
- #The author is Graeme Blake. Contact graemeblake at gmail if you have any questions
- ##################################################################
- #OUTLINE:
- #This program almost certainly violates best practices.
- #But it works.
- #Its a series of function calls. The functions pass control amongst themseleves.
- #The program goes deeper and deeper, and there are many infinite loops.
- #This doesn't matter, as the program's needs aren't great.
- #A global variable tracks input, and the student score on various exercies.
- #There is an option to input a score code to return to a previous session.
- #The program is quite extensible. New modules can be added by modifying the following things:
- #*The list of topics, and the dictionary of points.
- #*The help function, if necessary. Also add text for new help topics.
- #*The global commands function, for new topics.
- #That's it, unless a new feature is added.
- #The func() function is quite important. It's called whenever the user
- #has entered input. If necessary, it redirects to the appropriate function.
- #Otherwise it does nothing, and the current function continues.
- ##################################################################
- #LIBRARIES
- ##################################################################
- import math #I don't think I need this
- import random
- ##################################################################
- #GLOBAL VARIABLES
- ##################################################################
- topic_list = ["assignment", "arrays"] #This is the list of exercise topics
- points = {"assignment1":0, "assignment2": 0, "arrays1": 0, "arrays2": 0, "arrays3":0} #This tracks a student's score for each topic
- input = "" #Global variable to track last student response
- global max_score
- max = 9
- ##################################################################
- #CONTROL FLOW FUNCTIONS
- ##################################################################
- #INPUT2#
- #This is a global variable to get user input
- def input2():
- global input
- input = raw_input(">> ")
- return
- ##################################################################
- #PRINT_SCORE#
- #This prints out a score code for the user.
- def print_score():
- print "\nSCORE CODE"
- print "\nHere is your score code.\nWrite it down to continue your session later:\n"
- score = ''
- for e in points:
- score += "%d" % points[e]
- print "\nYour score code is: %s" % score
- #This lets a user enter a score code which
- #sets the values of the points dictionary
- ##################################################################
- #ENTER_CODE#
- def enter_code():
- print "\nSCORE CODE ENTRY"
- print "\nPlease enter your score code\n"
- length = len(points)
- while 1:
- input2()
- func()
- try:
- if eval('1' + input) >= 0 and len(input) == length: #The '1' corrects for an error
- i = 0
- for e in points:
- points[e] = eval(input[i])
- i += 1
- print "\nSuccess! Your code worked."
- break
- else:
- print "\nplease enter a real score code.\n\nThe score code should be a positive whole number, %d digits long" % length
- except:
- print "\nplease enter a real score code.\n\nThe score code should be a positive whole number, %d digits long" % length
- ##################################################################
- #MAIN#
- #This tells the user where they are, and asks them to choose a topic.
- def main():
- print """
- MAIN MENU
- You're at the main menu.
- Here is the list of topics:\n
- %s
- Type the topic name for exercies, or type help for more info.\n""" % topic_list
- while 1:
- input2()
- func()
- if input == "arrays":
- arrays()
- elif input == "assignment":
- assignment()
- else:
- print """
- Sorry, I'm just a dumb computer, I don't understand typos.
- Capital letters are also difficult.
- Type "help" for help.
- Type "main" to go to the main menu.
- """
- return
- ##################################################################
- #HELP#
- #This enters the help menu for topics. The func commands lets the user escape.
- def help():
- print """
- HELP MENU
- Type any of the following commands to learn more about a topic.
- Type main to return to the main menu.
- \n\nassignment
- \narrays
- \ntypos
- \npoints
- \nsaving progress
- """
- while 1:
- input2()
- func()
- if input == "assignment":
- assignment_help()
- elif input == "arrays":
- arrays_help()
- elif input == "typos":
- typos_help()
- elif input == "points":
- points_help()
- elif input == "saving progress":
- saving_progress_help()
- else:
- print """
- I didn't recognize that input.
- Type a topic name for information about that topic.
- Type "help" for the list of topics.
- Type "main" to return to the main menu.
- """
- ##################################################################
- #FUNC#
- #At each interval, this checks if the user wanted to go to one of the other menus.
- #If not, the function returns control.
- def func():
- if input == "help":
- help()
- elif input == "main":
- main()
- elif input == "score":
- print_score()
- main()
- elif input == "code":
- enter_code()
- main()
- return
- ##################################################################
- #HELP FUNCTIONS
- #This is the text for the information available in the help functions
- ##################################################################
- #ASSIGNMENT_HELP#
- def assignment_help():
- print """
- ASSIGNMENT HELP
- Assignment gives a value to a variable. For example:
- \nx = 4
- \nThe line above sets the value of x equal to 4. You can change the value of x.
- Let's try something more complicated. Write these on paper if you're unsure; it helps.
- \nx = 37
- x = x + 3
- x += 2
- \nx now equals 42.
- First, we set x to 37.
- Then we set x equal to the value of x (37) + 3, which is 40.
- += means we take the value of the variable, and add the number on the right.
- x was 40, so now we add 2 to x.
- x += 2 and x = x + 2 are the same operation. Use whichever you prefer.
- You can do all sorts of fancy math with assignment. For instance:
- x = 4
- y = 7
- z = x * y
- z now equals 28.
- Note that Z does not exist. Capital letters matter in variable names.
- """
- ##################################################################
- #ARRAYS_HELP#
- def arrays_help():
- print """
- ARRAYS HELP
- A list (also called an array) holds a series of values. For example:
- numbers = [1, 2, 3, 4, 5]
- You access list locations by using square brackets
- and a number for the location.
- Confusingly, lists start from the number zero.
- So numbers[0] = 1.
- numbers[4] = 5
- numbers[5] doesn't exist.
- We could expand our list. For example, we can add a number
- using the append function.
- numbers.append(54) -->
- print numbers
- [1, 2, 3, 4, 5, 54]
- You can measure the length of a list with the len() function.
- print len(numbers)
- 6
- You can also select subsections of lists. For example:
- print numbers [2:4]
- [3, 4]
- Think of it as starting from 2, and going all the way up to the edge of 4,
- but not actually including number[4]. So only numbers[2] and numbers[3]
- are selected.
- Lists can include strings too. In fact, you can mix numbers and strings.
- mix = ["Udacity", "is", 2938, "?"]
- That's a legal list, though it's certainly odd.
- Lastly, lists can be multi-dimensional. This list has three objects:
- list = [1, [2,3,5], 70]
- The second object is a list. So:
- list[0] = 1
- list[2] = 70
- list[1] = [2,3,5]
- What if you want to access 5? Enter the position of five in the list:
- list[1][2] = 5
- """
- return
- ##################################################################
- #POINTS_HELP#
- def points_help():
- print """
- POINTS HELP
- Your points are stored in a simple dictionary. Each topic has its own key.
- If you get a question right, you'll get a point for that topic.
- Get a question wrong, and your points get cut in half.
- If the computer doesn't understand your answer, you lose a point.
- Get nine points in a topic, and you've mastered it.
- You can get a score code for your points. Type "score" at any time.
- When you restart a session, type "code" to re-enter your score code.
- It would be very easy to game this system, but why bother?
- """
- return
- ##################################################################
- #SAVING_PROGRESS#
- def saving_progress_help():
- print """"
- SAVING PROGRESS HELP
- At any time, type "score" to get a score code.
- When you restart, you can type "code" to enter your code and resume
- your session.
- Both the "score" and "code" commands return you to the main menu afterwards.
- """
- return
- ##################################################################
- #TYPOS_HELP#
- def typos_help():
- print """
- TYPOS HELP
- I've tried to program for all types of input. But I don't always know how.
- So try and type things in a normal way.
- For example:
- list.append(42)
- list.append( 42 )
- list.append( 42 )
- Those are all legal ways to append 42 to list. But I've only coded the
- program to accept the first too.
- Likewise, I usually don't accept:
- a = 6
- Even though that is a way to set a to 6. Some exercises may accept this,
- but don't count on it.
- """
- return
- ##################################################################
- #EXERCISE FUNCTIONS
- ##################################################################
- #Functions are below, by Category
- ##################################################################
- #ARRAY FUNCTIONS
- ##################################################################
- #ARRAYS#
- #This just directs the user to choose the level of difficulty
- def arrays():
- print """
- Would you like to do array exercises level 1, 2 or 3?
- Enter 1, 2 or 3.
- """
- while 1:
- input2()
- func()
- if input == "1" or input == "arrays1":
- arrays1()
- elif input == "2" or input == "arrays2":
- arrays2()
- elif input == "3" or input == "arrays3":
- arrays3()
- else:
- print """
- Oops. Please enter 1, 2 or 3. Or type "main" to go back to the main menu.\
- \nType "help" for a list of commands.
- """
- ##################################################################
- #ARRAYS LEVEL 1#
- #This tests whether the user knows how to access elements in an array#
- def arrays1():
- print """
- ARRAYS ONE INTRO
- THis will teach you how to access locations in an array.
- """
- while 1:
- list = []
- length = random.randint(3,12)
- for i in range(0, length):
- list.append(random.randint(0, 28))
- loc = random.randint(0, length - 1)
- location = "list[%d]" % loc
- print "\nQUESTION:"
- print "\nhere is a list of objects called \"list\":"
- print "\nlist = %s (length of list = %d)" % (list, length)
- print "which list location has this value: %d" % eval(location)
- print "\nenter how you would access that location. \nThere may be more than one possibility.\n"
- if points["arrays1"] >= max:
- print "\nYou've mastered this exercise.\nType \"main\" to try something else.\n\n(Feel free to keep going, there's another question above.)"
- while 1:
- input2()
- func()
- try:
- if input == "new":
- break
- elif eval(input) == list[loc]:
- print "\n\nCORRECT"
- if points["arrays1"] < 9:
- points["arrays1"] += 1
- if points["arrays1"] != 1:
- print "You have %d points!" % points["arrays1"]
- elif points["arrays1"] == 1:
- print "You have %d point!" % points["arrays1"]
- break
- else:
- points["arrays1"] /= 2
- print "\noops, that's not right."
- if points["arrays1"] != 1:
- print "You have %d points!" % points["arrays1"]
- elif points["arrays1"] == 1:
- print "You have %d point!" % points["arrays1"]
- print "\n\nlist = %s\n%s equals %d.\
- \nYou were supposed to find which location has the value %d."\
- % (list, input, eval(input), list[loc])
- print "\nTry again, or type \"new\" for another question"
- except:
- if points["arrays1"] > 0:
- points["arrays1"] -= 1
- if points["arrays1"] != 1:
- print "You have %d points!" % points["arrays1"]
- elif points["arrays1"] == 1:
- print "You have %d point!" % points["arrays1"]
- print "I didn't recognize that input."
- print "\nTry again, or type \"new\" for another question"
- ##################################################################
- #ARRAYS LEVEL 2#
- def arrays2():
- print """
- ARRAYS TWO INTRO
- This will teach you a few important functions for using arrays.
- """
- while 1:
- type = random.randint(1,5)
- if type == 1: #length function
- print "\n QUESTION:"
- print "\nHow do you measure the length of this list?"
- list = []
- length = random.randint(3,12)
- for i in range(0, length):
- list.append(random.randint(0, 28))
- print "list = %s" % list
- while 1:
- if points["arrays2"] >= max:
- print "\nYou've mastered this exercise.\nType \"main\" to try something else.\n\n(Feel free to keep going, there's another question above.)"
- input2()
- func()
- try:
- if input == "new":
- break
- elif eval(input) == length and input != str(length): #disallow simply typing length
- print "\n\nCORRECT"
- if points["arrays2"] < 9:
- points["arrays2"] += 1
- if points["arrays2"] != 1:
- print "You have %d points!" % points["arrays2"]
- elif points["arrays2"] == 1:
- print "You have %d point!" % points["arrays2"]
- break
- else:
- print "\noops, that's not right.\n\nUse the len() function\
- \ne.g. len(list) --> %d" % length
- points["arrays2"] /= 2
- break
- except:
- print "\noops, that's not right.\n\nUse the len() function\
- \ne.g. len(list) --> %d" % length
- if points["arrays1"] > 0:
- points["arrays1"] -= 1
- break
- if type == 2: #sub-strings
- print "\n QUESTION:"
- print "\nHow do you select the string \"Udacity\" from this array?"
- list = []
- length = random.randint(1,4)
- for i in range(0, length):
- list.append(random.randint(0, 28))
- for i in "Udacity":
- list += i
- length = random.randint(1,4)
- for i in range(0, length):
- list.append(random.randint(0, 28))
- print "list = %s" % list
- while 1:
- if points["arrays2"] >= max:
- print "\nYou've mastered this exercise.\nType \"main\" to try something else.\n\n(Feel free to keep going, there's another question above.)"
- input2()
- func()
- try:
- if input == "new":
- break
- elif eval(input) == ['U', 'd', 'a', 'c', 'i', 't', 'y']:
- print "\n\nCORRECT\n"
- if points["arrays2"] < 9:
- points["arrays2"] += 1
- if points["arrays2"] != 1:
- print "You have %d points!" % points["arrays2"]
- elif points["arrays2"] == 1:
- print "You have %d point!" % points["arrays2"]
- break
- else:
- print "\noops, that's not right.\n\nHere is what you selected: %s" % eval(input)
- print "\nTry again, or type \"new\" for another question"
- points["arrays2"] /= 2
- except:
- print "I didn't recognize that input."
- print "\nTry again, or type \"new\" for another question"
- if points["arrays2"] > 0:
- points["arrays2"] -= 1
- break
- if type == 3: #appending
- print "\n QUESTION:"
- print "\nHow do you modify this list to add 42 at the end?"
- list = []
- length = random.randint(3,12)
- for i in range(0, length):
- list.append(random.randint(0, 28))
- print "list = %s" % list
- while 1:
- if points["arrays2"] >= max:
- print "\nYou've mastered this exercise.\nType \"main\" to try something else.\n\n(Feel free to keep going, there's another question above.)"
- input2()
- func()
- try:
- if input == "new":
- break
- elif input == "list.append(42)" or input == "list.append( 42)" or input == "list.append(42 )" or input == "list.append( 42 )":
- print "\n\nCORRECT"
- if points["arrays2"] < 9:
- points["arrays2"] += 1
- if points["arrays2"] != 1:
- print "You have %d points!" % points["arrays2"]
- elif points["arrays2"] == 1:
- print "You have %d point!" % points["arrays2"]
- break
- else:
- print "\noops, that's not right.\n\nUse the append() function\
- \ne.g. list.append(42)"
- points["arrays2"] /= 2
- break
- except:
- print "I didn't understand that input"
- print "\nTry again, or type \"new\" for another question"
- if points["arrays2"] > 0:
- points["arrays2"] -= 1
- ##################################################################
- #ARRAYS LEVEL #3
- def arrays3(): #multi-dimensional arrays
- print """
- ARRAYS 3 INTRO
- This will teach you multi-dimensional arrays.
- """
- while 1:
- list = []
- length = random.randint(3,9)
- for i in range(0, length):
- list.append([])
- for e in range(random.randint(1,4)):
- list[i].append(random.randint(0, 28))
- loc1 = random.randint(0, length - 1)
- loc2 = random.randint(0, len(list[loc1]) - 1)
- location = "list[%d][%d]" % (loc1, loc2)
- print "\n QUESTION:"
- print "\nhere is a list of objects called \"list\":"
- print "\nlist = %s \n(length of list = %d)" % (list, length)
- print "which list location has this value: %d" % eval(location)
- print "\nenter how you would access that location. \nThere may be more than one possibility.\n"
- if points["arrays3"] >= max:
- print "\nYou've mastered this exercise.\nType \"main\" to try something else.\n\n(Feel free to keep going, there's another question above.)"
- while 1:
- input2()
- func()
- try:
- if input == "new":
- break
- elif input != '%s' % str(list[loc1][loc2]) and eval(input) == list[loc1][loc2]:
- print "\n\nCORRECT"
- if points["arrays3"] < 9:
- points["arrays3"] += 1
- if points["arrays3"] != 1:
- print "You have %d points!" % points["arrays3"]
- elif points["arrays3"] == 1:
- print "You have %d point!" % points["arrays3"]
- break
- else:
- print "\noops, that's not right.\n\nlist = %s\n%s equals %d.\
- \nYou were supposed to find which location has the value %d."\
- % (list, input, eval(input), list[loc1][loc2])
- print "\nTry again, or type \"new\" for another question"
- points["arrays3"] /= 2
- except:
- print "I didn't recognize that input."
- print "\nTry again, or type \"new\" for another question"
- if points["arrays3"] > 0:
- points["arrays3"] -= 1
- ##################################################################
- #ASSIGNMENT FUNCTIONS
- ##################################################################
- def assignment():
- print """
- Would you like to do assignment exercises level 1 or 2?
- Enter 1 or 2.
- """
- while 1:
- input2()
- func()
- if input == "1" or input == "assignment1":
- assignment1()
- elif input == "2" or input == "assignment2":
- assignment2()
- else:
- print """
- Oops. Please enter 1 or 2. Or type "main" to go back to the main menu.\
- \nType "help" for a list of commands.
- """
- return
- ##################################################################
- #ASSIGNMENT One
- ##################################################################
- #This tests very basic assignment concepts.
- def assignment1():
- print """
- ASSIGNMENT ONE INTRO
- This will teach you the basic assignment operators.
- You'll also learn multiple assignment.
- """
- while 1:
- a = random.randint(0,15)
- b = random.randint(0,15)
- c = random.randint(0,15)
- d = random.randint(0,15)
- e = random.randint(0,15)
- array = ["a", "b", "c", "d", "e"]
- num1 = array[random.randint(1, len(array) - 1)]
- num2 = array[random.randint(1, len(array) - 1)]
- num3 = array[random.randint(1, len(array) - 1)]
- num4 = array[random.randint(1, len(array) - 1)]
- num5 = array[random.randint(1, len(array) - 1)]
- num6 = array[random.randint(1, len(array) - 1)]
- num7 = array[random.randint(1, len(array) - 1)]
- num8 = array[random.randint(1, len(array) - 1)]
- question = random.randint(1,4)
- if question == 1: #basic assignment
- print """
- QUESTION:
- a = %d\n
- b = %d
- c = %d
- d = %d
- e = %d
- """ %(a, b, c, d, e)
- print "How would you set a equal to %s" % num1
- a = eval(num1)
- if points["assignment1"] >= max:
- print "\nYou've mastered this exercise.\nType \"main\" to try something else.\n\n(Feel free to keep going, there's another question above.)"
- input2()
- func()
- if input == "a = %s" %num1 or input == "a = %d" % eval(num1):
- print "CORRECT"
- points["assignment1"] += 1
- if points["assignment1"] != 1:
- print "You have %d points!" % points["assignment1"]
- elif points["assignment1"] == 1:
- print "You have %d point!" % points["assignment1"]
- else:
- print "Sorry, that's not right. Make sure it's not a typo.\
- You should have typed \"a = %s" % num1
- points["assignment1"] /= 2
- if question == 2: #multiple assignment
- copy = []
- copy += array
- print """
- QUESTION:
- a = %d\n
- b = %d
- c = %d
- d = %d
- e = %d
- """ %(a, b, c, d, e)
- print """
- a, %s = %s, %s
- What is the value of a?
- """ % (num1, num2, num3)
- a = eval(num2)
- if points["assignment1"] >= max:
- print "\nYou've mastered this exercise.\nType \"main\" to try something else.\n\n(Feel free to keep going, there's another question above.)"
- input2()
- func()
- try:
- if input == "a = %s" %num2 or input == "a = %d" %eval(num2) or eval(input) == a:
- print "CORRECT"
- points["assignment1"] += 1
- if points["assignment1"] != 1:
- print "You have %d points!" % points["assignment1"]
- elif points["assignment1"] == 1:
- print "You have %d point!" % points["assignment1"]
- else:
- print "Sorry, that's not right. Make sure it's not a typo.\
- \"a = %d" % num2
- points["assignment1"] /= 2
- except:
- print "Sorry, didn't understand that."
- if points["assignment1"] > 0:
- points["assignment1"] -= 1
- ##################################################################
- #ASSIGNMENT TWO
- ##################################################################
- #This tests whether students can visualize changes in variables through
- #assignment.
- #This should probably be module three, with a second module covering
- #the arithmetic operations.
- def assignment2():
- print """
- ASSIGNMENT TWO INTRO
- This tests how well you understand assignment and it's operations.
- You'll see a list of variables, and a list of operations.
- Find the final value of a.
- """
- while 1:
- a = random.randint(0,15)
- b = random.randint(0,15)
- c = random.randint(0,15)
- d = random.randint(0,15)
- e = random.randint(0,15)
- array = ["a", "b", "c", "d", "e"]
- num1 = array[random.randint(1, len(array) - 1)]
- num2 = array[random.randint(1, len(array) - 1)]
- num3 = array[random.randint(1, len(array) - 1)]
- num4 = array[random.randint(1, len(array) - 1)]
- print """
- QUESTION:
- What is the value of a?:\n
- a = %d\n
- b = %d
- c = %d
- d = %d
- e = %d
- """ %(a, b, c, d, e)
- question = random.randint(1,4)
- if question == 1:
- print """
- a = %s + %s
- a += %s
- b *= %s
- a -= 5
- """ % (num1, num2, num3, num4)
- a = eval(num1) + eval(num2)
- a += eval(num3)
- a -= 5
- elif question == 2:
- print """
- a = %s - %s
- a *= 3
- q = %s * 74
- a = %s - 7
- """ % (num1, num2, num3, num4)
- a = eval(num4) - 7
- elif question == 3:
- print """
- r = %s + %s
- a += %s
- b *= %s
- a = a + (r * 0) + 21
- """ % (num1, num2, num3, num4)
- a += eval(num3)
- a += 21
- elif question == 4:
- print """
- a -= a
- a *= %s
- a -= %s
- a *= -3
- """ % (num1, num2)
- a = 0
- a -= eval(num2)
- a *= -3
- if points["assignment2"] >= max:
- print "\nYou've mastered this exercise.\nType \"main\" to try something else.\n\n(Feel free to keep going, there's another question above.)"
- input2()
- func()
- if input == "a = %d" % a and input != 'a':
- print "CORRECT"
- points["assignment2"] += 1
- if points["assignment2"] != 1:
- print "You have %d points!" % points["assignment2"]
- elif points["assignment2"] == 1:
- print "You have %d point!" % points["assignment2"]
- elif input == "%d" % a:
- print "CORRECT"
- points["assignment2"] += 1
- if points["assignment2"] != 1:
- print "You have %d points!" % points["assignment2"]
- elif points["assignment2"] == 1:
- print "You have %d point!" % points["assignment2"]
- else:
- print "Sorry, that's not right. Make sure it's not a typo.\na = %d" % a
- points["assignment2"] /= 2
- ##################################################################
- #START OF PROGRAM
- #Just prints an
- #intro and calls the main function
- #The user must press enter, so they have a chance to notice the intro text.
- ##################################################################
- print """
- INTRO
- Welcome to CS 101 python practice. This program is meant to help you
- learn programming by repeating the basic concepts.
- \nYou can go through a series of exercises. As you get questions right,
- the computer will let you know when it's time to move on.
- INSTRUCTIONS
- \nChoose an exercise by typing in its name.
- If you want to know more, type help for a list of topics.
- If you have a progess code to restart your session, type code to enter it.
- If you want to get a score code at any time type \"score\".
- However, this will cause you to leave your current exercise
- and return to the main menu afterwards.
- SCROLL UP TO SEE THE INTRO
- You can type "main", "help", "score" and "code"
- to access those functions AT ANY TIME.
- Type anything to start.
- """
- raw_input()
- while 1:
- main()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement