values from function help needed

Discussion in 'Python' started by trade19, Mar 29, 2014.

  1. trade19

    trade19 New Member

    Joined:
    Mar 29, 2014
    Messages:
    6
    Likes Received:
    0
    Trophy Points:
    0
    Hi I need help understanding the code below. This code is used to compare some numbers
    Example: 02,10,26,33,48,,,,,, 15 is a WIN number and 02,11,24,34,50, ,,,,,15is a DRAWN number.
    output is

    02,11,24,34,50, ,,,,,15 (1,1) these ones are due to the matching numbers 02 and 15
    I f there were no matches it would be (0,0)
    I can run the program and read that there was a win or loss but I don't know how to evaluate with code a win or loss.

    Example2:Up to 5 numbers can be purchased like below
    WIN# 02, 10, 26, 33, 48,,,,,, 15

    07, 13, 26, 31, 46,,,,,, 17 (0,0)
    02, 11, 24, 34, 50, ,,,,,15 (1,1)
    04, 18, 25, 38, 49,,,,,, 27 (0,0)
    02, 10, 26, 37, 49,,,,,, 10 (3,0)
    08, 14, 27, 33, 38,,,,,, 26 (1,0)
    this is the output for 5 numbers
    I can read this and see what numbers have won/lost
    the (3,0)and the (1,1) would win
    I want to generate a message that the ticket won and I don't know how to manipulate the (0,0)part.

    If you need the complete program let me know.




    Code:
    def score(a, b):
            return (len(set(a[:5]) & set(b[:5])), 1 if (a[-1] == b[-1]) else 0)   
        print()
        
        for a in s:               
            print(a, score(a, target))
            label1 = Label(aApp, text=(a, score(a, target)), fg='blue')
            label1.pack(side=BOTTOM, expand=NO, padx=3, pady=1, ipadx=1, ipady=1)
     
  2. trade19

    trade19 New Member

    Joined:
    Mar 29, 2014
    Messages:
    6
    Likes Received:
    0
    Trophy Points:
    0
    error in 07, 13, 26, 31, 46,,,,,, 17 (1,0) sorry
     
  3. trade19

    trade19 New Member

    Joined:
    Mar 29, 2014
    Messages:
    6
    Likes Received:
    0
    Trophy Points:
    0
    Here is the program It will run if you create a text file the one used here is output.txt


    Code:
    from tkinter import*
    import sys, random, tkinter
    ticket_price = 2
    i = 0
    g = ''
    total = 0
    comp_a = ' '
    comp_b = ' '
    score = 0
    drawing2 = ''
    drawing = ''
    target = ''
    s = ''
    sx = ''
    a = ''
    b = ''
    test_score = ''
    
    
    def calculatetotals():    
        valid = set('12345')
        ticket_entry = tik.get()
        if ticket_entry not in valid:
            print('We permit values in {}.'.format(valid))
            label4 = Label(aApp, text='We permit values in 1, 2, 3, 4 or 5!',
                           fg='blue').grid(row=7, column=1)
    
        else:
            label4 = Label(aApp, text='                                                                           ', fg='blue').grid(row=7, column=1)
            mytik = int(ticket_entry)
            total = mytik*ticket_price
            Label(aApp, text="You purchased:$%.2f \n" % total).grid(row=7, column=1)
            Label(aApp, text="\nNumber Of Tckets: %.f\n" % mytik).grid(row=6, column=1)
            Button(aApp, text="Click Here To Draw Your Tickets!",  fg='blue',  bg='white',  command=nextx).grid(row=8, column=1)
    
            
    aApp = Tk()                           
    aApp.geometry('600x400+75+75')
    aApp.title("LOTTO")
    tik = StringVar()
    label1 = Label(aApp, text="Welcome To Lotto.", fg='blue')
    label1.grid(row=0, column=2)
    label2 = Label(aApp, text="Tickets Are $ {:.2f}Each.".format(ticket_price), fg='red')
    label2.grid(row=1, column=1)
    label3 = Label(aApp, text="How Many Would You Like?", fg='black')
    label3.grid(row=2, column=1)
    
    mytik = Entry(aApp, textvariable=tik)
    mytik.grid(row=2, column=2)
    
    button1 = Button(aApp, text="Your Purchse\nClick Here", fg='blue', command=calculatetotals)                 
    button1.grid(row=6, column=1)
    
    
    def nextx():
        Button(aApp, text="Click Here to see the new winning number.", fg='lightgreen',
               bg='black', command=nextz).grid(row=8, column=1)
        ticket_entry = tik.get()
        i = 0
        while i < int(ticket_entry):            
            L = list(range(1, 60))
            random.shuffle(L)        
            g = L[:5]
            g.sort()
            f = L[5:6]                           
            drawing = ' '.join([' '.join(zstr(G) for G in g), '-', zstr(f[0])])
            label5 = tkinter.Label(aApp, text=drawing)
            label5.pack(fill=X)
            
            comp_b = open("C:\output.txt", "a")
            s = comp_b.write(drawing)
            s = comp_b.write('\n')
            s = [line.split() for line in sx.strip().splitlines()]                   
            i += 1
            
            
    def nextz():
        Button(aApp, text='Push again to <Quit> :To see if you match Push <Compare> In The New Winning Number Window'
               , fg='yellow', bg='black', command=quit).grid(row=8, column=1)
        L = list(range(1, 60))
        random.shuffle(L)
        g = L[:5]
        g.sort()
        f = L[5:6]
        global drawing2
        global target
        drawing2 = ' '.join([' '.join(zstr(G) for G in g), '-', zstr(f[0])])
        target = drawing2
       
        aBpp = Tk()
        aBpp.geometry('240x75+230+260')
        aBpp.title(string="Winning Number")
        label6 = tkinter.Label(aBpp, text=drawing2).pack(padx=1, pady=2)
        Button(aBpp, text="quit", command=aBpp.quit).pack()
        Button(aBpp, text="Compare", command=compare).pack()
     
     
    def zstr(ob, width=2):
        return '{x:0>{width}s}'.format(x=str(ob), width=width)
    
    
    def compare():
        s = ''
        comp_a = open("C:\output.txt", "r")
        s = comp_a.read()
        s = [line.split() for line in s.strip().splitlines()]   
        target = drawing2.split()
        print('Purchased Numbers are', s)
        print('Win Number is ', target)
       
        def score(a, b):
            return (len(set(a[:5]) & set(b[:5])), 1 if (a[-1] == b[-1]) else 0)
      
        print()
        
        for a in s:
           
            print(a, ('score', score(a, target)))
            if (a,b):
                    print('score is 1')               
            else:
                    print ('score = 0')
                    
            label1 = Label(aApp, text=(a, score(a, target)), fg='blue')
            label1.pack(side=BOTTOM, expand=NO, padx=3, pady=1, ipadx=1, ipady=1)
            
            comp_a.close()
    killfile = open("C:\output.txt", "w")
    killfile.close()
    
    
    tkinter.mainloop()
    aBpp.mainloop()
    aApp.mainloop()
    
     
  4. trade19

    trade19 New Member

    Joined:
    Mar 29, 2014
    Messages:
    6
    Likes Received:
    0
    Trophy Points:
    0
    remove this part it is something I was working on and makes no sense.:)
    if (a,b):
    print('score is 1')
    else:
    print ('score = 0')
     

Share This Page

  1. This site uses cookies to help personalise content, tailor your experience and to keep you logged in if you register.
    By continuing to use this site, you are consenting to our use of cookies.
    Dismiss Notice