[Edu-sig] source code from SA:10648

kirby urner kirby.urner at gmail.com
Thu Jul 15 18:24:17 CEST 2010


Here is some of the code we will be using in class today.  After that,
a source code file from yesterday's class:


Some Games
"""

from random import randint, choice

# a list of (question, answer) tuples
quiz1 = [("What is the capital of Oregon?", "Salem"),
       ("Is Portland north of Salem?", "Yes"),
       ("Does I-5 go to Seattle?", "Yes"),
       ("Does I-5 go to Pendleton?",  "No")]

quiz2 = [("What is the capital of Washington State","Olympia"),
         ("Are kangaroos native to China?","No"),
         ("Who is the president of the USA?", "Obama"),
         ("What computer language is named for Monty Python?", "Python")]

quizzes = [quiz1, quiz2]  # list of possible quizzes

def askq(quiz = quiz1):
    score = 0
    possible = len(quiz)
    while len(quiz) > 0:
        pickone = randint(0, len(quiz)-1)
        print(quiz[pickone][0])
        answer = raw_input("Your answer? ")

        if answer.upper() == quiz[pickone][1].upper():
            # yes and Yes would both count (but not Y)
            print("Correct!")
            score = score + 1
        else:
            print("Correct answer was %s" % quiz[pickone][1])

        quiz.pop(pickone)

    print("Your score was %s out of a possible %s" % (score, possible))

def guessing(a=0,b=10):
    thenumber = randint(a,b)
    while True:
        print("I'm thinking of a number between %s and %s, what is it?" % (a,b))

        answer = raw_input("Guess (q to quit): ")

        if answer == "q":
            print("Thanks for trying, the number was %s" % thenumber)
            break # escape loop

        if not answer.isdigit():
            # trap non-integer answers
            print("That's not a number!")
            continue # loop again

        answer = int(answer)  # convert to integer
        if answer == thenumber:
            print("That's it!")
            break  # escape loop
        elif answer < thenumber:
            print("Try larger!")
        elif answer > thenumber:
            print("Try smaller!")

def mainmenu():
    while True:
        print("""
What game would you like to play?
1  Take a Quiz
2  Guess a Number
0  Exit""")
        sel = raw_input("Your selection? ")
        if sel == "0":
              print("OK, Good Bye")
              break
        elif sel == "1":
              # randomly choose a quize
              askq(choice(quizzes))
        elif sel == "2":
              guessing()
        else:
            print("Huh?  Please try again")


if __name__ == "__main__":
    # askq(quiz1)
    # guessing()
    mainmenu()


=======


from random import randint, random, choice

allcolors = [color.orange, color.red, color.magenta, color.yellow, color.blue,
             color.white, color.green]


def randcolor():
    return (random.random(), random.random(), random.random())

def junk(n=100):
    for i in range(n):
        draw = choice(["ball", "box"])
        if draw == "ball":
            manyspheres(1)
        else:
            manyboxes(1)

def manyboxes(n=100):
    """
    make random boxes....

    mybox = box(pos=(x0,y0,z0), length=L, height=H, width=W)
    """
    for i in range(n):
        thebox = box(
            pos=(randint(-100,100), randint(-100,100), randint(-100,100)),
            size = (randint(1, 10),randint(1,10),randint(1,10)) ,
            color = randcolor())

def manyspheres(n=100):
    for i in range(n):
        theball = sphere(
            pos=(randint(-100,100),randint(-100,100),randint(-100,100)),
            radius = randint(1, 10),
            color = randcolor())

def xyz(n=10):
    posx = cylinder(radius = 0.1, pos=(0,0,0), axis = (n,0,0), color =
color.white)
    negx = cylinder(radius = 0.2, pos=(0,0,0), axis = (-n,0,0), color
= color.white)
    posy = cylinder(radius = 0.3, pos=(0,0,0), axis = (0,n,0), color =
color.white)
    negy = cylinder(radius = 0.4, pos=(0,0,0), axis = (0,-n,0), color
= color.white)
    posz = cylinder(radius = 0.5, pos=(0,0,0), axis = (0,0,n), color =
color.white)
    negz = cylinder(radius = 0.6, pos=(0,0,0), axis = (0,0,-n), color
= color.white)

def circle():
    print "Making a circle"
    theball = sphere(pos=(2,0,0), radius = 1, color = color.red)
    for i in range(10000):
        newx = 2 * math.cos(math.radians(i))
        newy = 2 * math.sin(math.radians(i))
        theball.pos = (newx, newy, 0)
        rate(30)

def clock():
    bighand = cylinder(radius = 0.1, pos=(0,0,0), axis = (0,10,0),
color = color.green)
    littlehand = cylinder(radius = 0.1, pos=(0,0,0), axis = (0,5,0),
color = color.white)
    clock = ring(pos=(0,0,0), axis = (0,0,1), radius = 10, color =
color.red, thickness = 0.1)
    for i in range(0, -10000, -1):
        newx = 10 * math.cos(math.radians(90 + i ))
        newy = 10 * math.sin(math.radians(90 + i ))
        bighand.axis = (newx, newy, 0)
        newx = 5 * math.cos(math.radians(90 + i/12.))
        newy = 5 * math.sin(math.radians(90 + i/12.))
        littlehand.axis = (newx, newy, 0)
        rate(30)


# student code sent by email from another workstation,
# appended and converted into a function (vs. top-level)

from visual import *
import random
window = display(title="Random Sphere", width = 1024, height = 768)
window.select()

class mysphere():
	def __init__(self, my_color, my_pos, my_name, my_radius):
		mysphere_name = sphere(pos = my_pos,
                                       color = my_color, radius = my_radius)

def test2():
    t = 0		
    while t < 100000:
            my_color = random.choice(((255, 255, 255), (255, 255, 0),
(255, 0, 0), (255, 0, 255), (0, 255, 255), (0, 0, 255), (255, 0, 0)))
            my_pos = (random.randrange(-1000, 1000),
                      random.randrange(-1000, 1000),
                      random.randrange(-1000,1000))
            my_radius = random.randrange(5, 20)
            new_mysphere = mysphere(my_color, my_pos, t, my_radius)
            t += 1
            rate(50)


if __name__ == "__main__":
    # test2()
    junk()
    # manyboxes()
    # manyspheres()
    # xyz()
    # circle()
    # clock()


More information about the Edu-sig mailing list