woooee's Profile User Rating: -----

Reputation: 15 Tradesman
Group:
Active Members
Active Posts:
66 (0.13 per day)
Joined:
21-November 12
Profile Views:
390
Last Active:
User is offline Today, 09:17 AM
Currently:
Offline

Previous Fields

Dream Kudos:
0
Icon   woooee has not set their status

Posts I've Made

  1. In Topic: Need help, have to make a tic tac toe using turtle!

    Posted 9 Apr 2014

    This is a Tic-Tac-Toe game that I started but probably will never finish as it is more trouble than it is worth, and is much easier to do using buttons in Tkinter. Any how, perhaps you can get some ideas. It does not test for a winner so will only exit once all of the squares are filled, and prints "tie" whether it is or not.
    import turtle
    
    class TicTacToe():
        def __init__(self):
            self.scr = turtle.Screen()
            self.t = turtle.Turtle()
    
            self.player = True  ## X = True, O = False
            self.button_list=[[0, 0, False], [90, 0, False], [180, 0, False],
                           [0, 90, False], [90, 90, False], [180, 90, False],
                           [0, 180, False], [90, 180, False], [180, 180, False]]
            for x, y, occupied in self.button_list:
                self.draw_button(x, y)
    
            turtle.onscreenclick(self.print_choice)
            turtle.mainloop()
    
        def check_for_tie(self):
            """ see if all squares are filled 
            """
            for each_list in self.button_list:
                if each_list[2] == False:
                    return
            print "tie"
            turtle.bye()
    
        def draw_button(self, start_x, start_y):
            """ draw a button 90X90 given the x and y coordinates
                for the corner
            """
            self.t.up()
            self.t.goto(start_x, start_y)
            self.t.down()
            for ctr in range(4):
                self.t.forward(90)
                self.t.left(90)
    
        def draw_o(self, x, y):
            self.t.up()
            self.t.goto(x+45, y+18)
            self.t.down()
            self.t.circle(25)
    
        def draw_x(self, x, y):
            self.t.up()
            self.t.goto(x+30, y+30)
            self.t.down()
            self.t.goto(x+60, y+60)
    
            self.t.up()
            self.t.goto(x+30, y+60)
            self.t.down()
            self.t.goto(x+60, y+30)
    
        def print_choice(self, x, y):
            ctr = 1
            for each_list in self.button_list:
                x1 = each_list[0]
                y1 = each_list[1]
                if (x1 <= x <= x1+90) and \
                   (y1 <= y <= y1+90) and \
                   (False == each_list[2]):
                    print "button number", ctr
                    if self.player:
                        self.draw_x(x1, y1)
                    else:
                        self.draw_o(x1, y1)
                    self.player = not self.player
                    each_list[2] = True
                    self.check_for_tie()
                    return
                ctr += 1
            print "Not a valid choice --> try again"
    
    choice = "y"
    while choice.startswith("y"):
        TTT = TicTacToe()
        choice = raw_input('Play another game ("y" or "yes")? ') 
        choice = choice.lower() 
    
  2. In Topic: a function returns the sum of two arguments (like x and y)

    Posted 31 Mar 2014

    Python's plus operator will return correctly for numbers or stings. The only error is for two different types. And please use spaces for indents instead of tabs and spaces.
    def addition(arg1, arg2):
        try:
            return arg1 + arg2   
        except:
            return None
    
    for val_1, val_2 in ((2, 3), ("A", "B"), (2, "A")):
        print val_1, val_2, "=",  addition(val_1, val_2) 
    
  3. In Topic: Bank teller program sqlite problem editing values/help

    Posted 20 Mar 2014

    You have to select the record, get the current balance, add the difference, and then update. Options #1 and #2 work in the following code AFAIK, don't know about the others.
    import sqlite3
    con = sqlite3.connect('cajero.sql')
    cur = con.cursor()
    
    ## I used an INT for the money amount
    ## because that is what you input
    cur.execute("CREATE TABLE IF NOT EXISTS Usuarios (Fname VARCHAR(20), Lname VARCHAR(20), account VARCHAR(6), carne INT, cantidad INT)")
    
    def print_all_recs():
        print "Printing all recs"
        cur.execute("select * from Usuarios")
        print cur.fetchall()
        print
    
    def menu():
        print"1. Depositar\n 2. Retirar\n 3. Balance\n 4. Ingresar al sistema \n 5.Salir"
        return int(raw_input("Ingrese la opcion "))
    
    print_all_recs()
    #programa principal
    opcion=0
    
    while opcion!=5:
        print"Bienvenidos al cajero automatico "
        opcion= menu()
        if opcion==4:
            first_name=raw_input("Ingrese su primer nombre ")
            last_name=raw_input("Ingrese su apellido ")
            number = 0
            OK=False
            while not OK:
                try:
                    number_str=raw_input("ingrese su numero de carne de ")
                    number = int(number_str)
                    OK = True
                except:
                    print "Enter a whole number only"
    
    ##        import sqlite3
    ##        con = sqlite3.connect('cajero.sql')
    ##        cur = con.cursor()
    ##        Fname = x
    ##        Lname = y
    ##        carne = r
            account = True
            cantidad=5000
    ##        p=cantidad
            cur.execute("""INSERT INTO Usuarios (Fname, Lname, account, carne,cantidad) VALUES
            (?,?,?,?,?)""" , (first_name, last_name, account, number ,cantidad))
            con.commit()#Do not forget this!!
    ##        cur.close()
    ##        con.close()
            opcion=0
    
    
        if opcion==2:
            dollars=int(raw_input("ingrese cantidad (dollars) "))
            number=int(raw_input("ingrese su numero de carne (account) "))
            
    ##        import sqlite3
    ##        con = sqlite3.connect('cajero.sql')
    ##        cur = con.cursor()
            cur.execute("SELECT * FROM Usuarios WHERE carne==:acc_num",
                       {"acc_num":number})
    
            one = cur.fetchone()
            if one:
                print one
                new_dollars = int(one[-1]) + dollars
                cur.execute("UPDATE Usuarios SET cantidad==:amt WHERE  carne==:acc_num",
                           {"acc_num":number, "amt":new_dollars})
    #        p=cantidad
                con.commit()
                print_all_recs()
            else:
                print "NOT on file"
    
        if opcion==1:
            j=int(raw_input("ingrese cantidad"))
            r=int(raw_input("ingrese su numero de carne de "))
    ##        import sqlite3
    ##        con = sqlite3.connect('cajero.sql')
    ##        cur = con.cursor()
            
            cur.execute("UPDATE Usuarios SET cantidad=" + str(p+j) + "   WHERE carne=" +str(r)+ """""")
            p=cantidad
            con.commit()
    
        if opcion==3:
            r=int(raw_input("ingrese su numero de carne de "))
    ##        import sqlite3        
    ##        con = sqlite3.connect('cajero.sql')
    ##        cur = con.cursor()
            cur.execute("""SELECT * FROM Usuarios WHERE carne=""" +str(r))
            one = cur.fetchone()
            print one
            print cantidad
    
    if con:
        con.close() 
    
  4. In Topic: Tkinter input link to certain data

    Posted 20 Mar 2014

    Post the code you have tried. It should be a simple listbox. Also, you can use a dictionary for the elements instead of 100 if statements, and then
    if choice in the_dictionary.
    Any time you use more than 2 or 3 if/elif statements you should be looking for a better way to do it.

    An example from some where on the web of a listbox.

    try:
        import Tkinter as tk     ## Python 2.x
    except ImportError:
        import tkinter as tk     ## Python 3.x
    
    class TestCallback:
       def __init__(self):
          self.top = tk.Tk()
          self.top.geometry( "100x100+10+10" )
          self.top.minsize( 200, 175 )
    
          ##------ Must Go Before ListBox???
          exit = tk.Button(self.top, text='Exit',
                 command=self.top.quit, bg='blue', fg='yellow' )
          exit.pack(side="bottom", fill="x", expand=1)
    
          self.create_listbox()
          self.top.mainloop()
    
    
       def create_listbox(self):
          self.listbox = tk.Listbox( self.top, height=6, width=20, font=('Fixed', 14) )
    
          lit = [ "aaa", "bbbbb", "ccccccc", "dd", "e", \
                  "fff", "ggggg", "hhhhhhh", "jj", "m", \
                  "nn", "ooo", "ppp"]
          for ctr, item in enumerate(lit):
              new_item = "%2d  %-10s" % (ctr, item)
              self.listbox.insert("end", new_item)
    
          self.listbox.pack(side="left")
          self.listbox.see(2)
          self.listbox.bind("<Double-Button-1>", self.test_callback)
    
    
       def test_callback(self, event):
          print self.listbox.curselection()
          self.value=self.listbox.curselection()[0]  ## assumes only one item selected
          self.listbox.destroy()
          self.lab=tk.Label(self.top, text="you chose "+str(self.value), bg="red")
          self.lab.pack()
    
    TC = TestCallback() 
    
  5. In Topic: click and Boolean logic with tkinter

    Posted 10 Mar 2014

    A boolean will work fine in this case. A simple example:
    try:
        import Tkinter as tk     ## Python 2.x
    except ImportError:
        import tkinter as tk     ## Python 3.x
    
    class ButtonsTest:
       def __init__(self):
          self.top = tk.Tk()
          self.top.title('Buttons Test')
          self.color_ctr = True
          self.butn = tk.Button(self.top, text="Press to Increment",
                    command=self.colors)
          self.butn.grid(row=1, column=0)
    
          self.labl = tk.Label(self.top, text="Changes Colors")
          self.labl.grid(row=0, column=0)
    
          tk.Button(self.top, text='Exit',
                 command=self.top.quit).grid(row=10,column=0)
    
          self.top.mainloop()
    
       def colors(self):
          this_color = "green"
          if self.color_ctr:
             this_color="red"
          self.butn.config(bg=this_color)
          self.labl.config(text=this_color)
          self.color_ctr = not self.color_ctr
    
    BT = ButtonsTest() 
    

My Information

Member Title:
D.I.C Head
Age:
Age Unknown
Birthday:
Birthday Unknown
Gender:

Contact Information

E-mail:
Private

Friends

woooee hasn't added any friends yet.

Comments

woooee has no profile comments yet. Why not say hello?