Okym
Okym

Reputation: 259

TypeError: '>' not supported between instances of 'function' and 'int'

I am trying to code a simple coinflip type game and when I run it I get the error:

if ticket > 50:
TypeError: '>' not supported between instances of 'function' and 'int'

and I don't really understand what I'm doing wrong. For me it seems everything to be fine but when I run it it gives me that error that I previosly wrote. Any help is highly appreciated. Here's my code:

def flip():
    if player_ticket > 50:
        if ticket > 50:
            result = Label(mainFrame, text="YOU WON!!!", fg="green")
            result.grid(row=3, columnspan=2)
        else:
            result = Label(mainFrame, text="YOU LOST!!!", fg="red")
            result.grid(row=3, columnspan=2)    

    else:
        if player_ticket < 50:
            if ticket < 50:
                result = Label(mainFrame, text="YOU WON!!!", fg="green")
                result.grid(row=3, columnspan=2)
            else:
                result = Label(mainFrame, text="YOU LOST!!!", fg="red")
                result.grid(row=3, columnspan=2)    

def ticket_heads():
    global player_ticket
    player_ticket = decimal.Decimal(random.randrange(0, 50))    

def ticket_tails():
    global player_ticket
    player_ticket = decimal.Decimal(random.randrange(50, 100))    

def ticket():
    global ticket
    ticket = decimal.Decimal(random.randrange(0, 100))    

heads_text = Label(mainFrame, text="Heads")
heads_text.grid(row=0, column=0)    

tails_text = Label(mainFrame, text="Tails")
tails_text.grid(row=0, column=1)    

heads_select = Button(mainFrame, text="Select", command=ticket_heads)
heads_select.grid(row=1, column=0)    

tails_select = Button(mainFrame, text="Select", command=ticket_tails)
tails_select.grid(row=1, column=1)    

flipit = Button(mainFrame, text="Flip It!", command=flip)
flipit.grid(row=2, columnspan=2)    

Upvotes: 2

Views: 30779

Answers (1)

Nae
Nae

Reputation: 15355

Your ticket variable gets overwritten by your function variable, ticket. Which makes you get an error meaning you can't compare oranges(functions) to apples(integers). Rename either one and it should work fine.

Either replace:

global ticket
ticket = decimal.Decimal(random.randrange(0, 100))

with:

global anything_but_ticket
anything_but_ticket = decimal.Decimal(random.randrange(0, 100))

or:

def ticket():

with:

def anything_but_ticket():

Upvotes: 7

Related Questions