Recreating the integer guessing game

Hello, fellow KNIMErs!

The following Python code (or something like it) is often found in learning textbooks. I’m building use cases to explain snippets (recipes?) for a group of my son’s friends who’ve been immersed in Python and want to better understand workflow development and understand equivalencies: how would I write this in KNIME?

I’ll be the first to admit that I have very little experience with the Widget nodes and building interfaces for input. Can someone help me with an example workflow that recreates this Python code?

def guessing_game():

answer = random.randint(0, 100)

while True:
    user_guess = int(input('What is your guess (random between 0-100): '))

    if user_guess == answer:
        print(f'Right! The answer is {user_guess}')
    if user_guess < answer:
        print(f'Your guess of {user_guess} is too low!')
        print(f'Your guess of {user_guess} is too high!')

Hi @lenexa_jayhawk , there are quite a few ways to do this in Knime, including using Python.

But based on what you wrote, it looks like you want to “better understand workflow development and understand equivalencies”, meaning you want something without Python, is that correct?

I present to you 2 ways (they are more or less the same - I’ll explain a bit further):

First of all, I defined the lower and upper bounds as variables (0 and 100) in case you want to change them when you are playing with the workflow, and this is done via the Variable Creator:

The interaction with Widgets is fully appreciated when running on the Front End, that is the web portal, which requires you to run on a Knime server. If you are running the workflow from Knime AP (back end), then it’s better to use Components if you want interactive input. I created a “What is your guess?” component for that.

This is what it looks like when you double click on it:

FYI, that Label “What is your guess (random between 0-100)” is dynamic and relies on the minimum and maximum values that you configure. The default value also defaults to the minimum value. Let’s say if you configure the min and max values to be 200 and 300 respectively, the label and default value will change automatically:

Now, to be able to run anything from this point (even with a Python script), you need to have one dummy record, hence the dummy row.

After that, it’s all about generating random numbers and comparing.

In Method 1, the Math Formula generates the number, the Rule Engine compares and the String Manipulation generates the correct wording for the results.

Basically in Method 2, all of the Method 1 is done via the Column Expression. Column Expression allows you to evaluate If statements and do manipulation all in the same node.

Here’s the workflow: Integer guessing game.knwf (23.3 KB)

EDIT: I added a piece of Java Snippet to popup the result, which may be a bit more fun:

You can attach the Java Snippet with whichever method you want to use.

This is what the popup looks like:

So basically to play the game:

  1. Set up your min and max (should not change frequently and currently set at 0 and 100)
  2. Take a guess via the Component
  3. Run the Java Snippet

Here’s the workflow with the popup: Integer guessing game.knwf (25.3 KB)


Hi @lenexa_jayhawk,

here comes another possible solution.

The interactive component:

And here comes the workflow:
guessingGame.knwf (119.5 KB)

Best Regards


I did not notice your while True loop and that your random generated value was outside the loop. So I am guessing that the game keep going with multiple guesses with the same generated value. If that is the case, my workflow is not doing this. It only allows 1 guess and if you guess again, it will generate a new number, meaning you only get 1 chance out of 101 (0 to 100) each time.

I’ve modified the workflow so that the guesses applies to the same generated number, that way you can keep guessing and keep playing the game. This is done by simply moving the input Component after the random number:

It looks a bit messy simply because the workflow has 2 methods, but if I separate them, it really looks like this:
Method 1:

Method 2:

Here’s the modified workflow: Integer guessing game.knwf (35.3 KB)

1 Like

@Andrew_Steel Thank you for this elegant solution. I was really struggling with component development and your component was exactly what I was looking for.

1 Like

@bruno29a Thanks for this alternative method. It highlights a variety of nodes I would not have considered before.

This topic was automatically closed 90 days after the last reply. New replies are no longer allowed.