the function is running again and again without any kind of loop

Viewed 51

I am making a rock paper scissor game but an error is coming.

from tkinter import *
from PIL import Image, ImageTk

def get_random_token():
    tokens = ['rock', 'paper', 'scissor']
    comp_token = random.choice(tokens)
    print(comp_token)
    return comp_token

def show_comp_token():
    if get_random_token() == 'rock':
        rock_token = ImageTk.PhotoImage(Image.open('./gallery/rock_token.png'))
        comp_label.configure(image=rock_token, width=80, height=80)
        comp_label.image = rock_token

    if get_random_token() == 'paper':
        paper_token = ImageTk.PhotoImage(Image.open('./gallery/paper_token.png'))
        comp_label.configure(image=paper_token, width=80, height=80)
        comp_label.image = paper_token

    if get_random_token() == 'scissor':
        scissor_token = ImageTk.PhotoImage(Image.open('./gallery/scissor_token.png'))
        comp_label.configure(image=scissor_token, width=80, height=80)
        comp_label.image = scissor_token

def logic():
    if get_user_token() == 'rock':
        if get_random_token() == 'rock':
            return 'user TIE'
        if get_random_token() == 'paper':
            return 'user LOST'
        if get_random_token() == 'scissor':
            return 'user WON'

    if get_user_token() == 'paper':
        if get_random_token() == 'rock':
            return 'user WON'
        if get_random_token() == 'paper':
            return 'user TIE'
        if get_random_token() == 'paper':
            return 'user LOST'

    if get_user_token() == 'scissor':
        if get_random_token() == 'rock':
            return 'user LOST'
        if get_random_token() == 'paper':
            return 'user WON'
        if get_random_token() == 'scissor':
            return 'user TIE'
def main_func():
        logic()

this is only some part of the program but the error is in this portion only. when I run the program and print comp_token in the function get random tokens six instances get printed but I only want one and this is causing a problem.

please help

paper
scissor
scissor
rock
rock
rock
1 Answers

In logic method, you are calling get_random_token method again and again(which contains a print statement). Instead what you should do is to call the method once and store the value in a variable and then check it. Also you should do the same of get_user_token method. Like this:

def logic():
    random_token = get_random_token()
    user_token = get_user_token()
    if user_token == 'rock':
        if random_token == 'rock':
            return 'user TIE'
        if random_token == 'paper':
            return 'user LOST'
        if random_token == 'scissor':
            return 'user WON'

    if user_token == 'paper':
        if random_token == 'rock':
            return 'user WON'
        if random_token == 'paper':
            return 'user TIE'
        if random_token == 'paper':
            return 'user LOST'

    if user_token == 'scissor':
        if random_token == 'rock':
            return 'user LOST'
        if random_token == 'paper':
            return 'user WON'
        if random_token == 'scissor':
            return 'user TIE'
Related