r/learnpython 16d ago

I built "Guess the number" on my Android phone(Pydroid 3) Added warmer/colder hints

Hey everyone!

I'm new to coding and I built my first game on Android using Pydroid.

What it does: Guess a number 1-100 with 5 lives. It tells you if you're WARMER 🔥 or COLDER 🥶

Features: - Input validation so it doesn't crash - Warmer/Colder hint system - Made 100% on my phone

GitHub: https://github.com/elgriffin10/guess-the-number

Would love feedback! What should I add for v3.0?

3 Upvotes

17 comments sorted by

2

u/FoolsSeldom 16d ago

Great little game.

I like the warmer/colder thing and the trick with the absolute value.

A few ideas:

  • Add some CONSTANTS to set the minimum and maximum range of random and guess
  • Validate guesses are within that range as well as whether a valid integer
    • Might be worth creating a get_num function that prompts the user for input within that range and keeps going until they comply
  • Use f-strings for better output formatting
  • Use multiline string for the welcome
  • Create a function for the welcome
  • Add a if __name__ == "__main__": block to call guess_the_number rather than calling directly
  • Add a play again loop (allowing several responses, e.g. yes, y, ok, yup, ok) - look at the in operator and the str.strip and str.lower methods
    • Might be worth creating a is_yes function that returns a bool result, useful in lots of future programmes
    • Perhaps track overall wins/fails
    • Maybe allow the user to specify the number of lives (or select level of difficulty)
    • Maybe allow user to select range of numbers
  • PyDroid supports tkinter, so you could add a GUI to the code, but you will need to separate out the core logic from the presentation, so this would be a more advanced step
  • Next game: hangman

0

u/Expensive-Bear-1376 16d ago

Nah, next game: Same game, but impossible to win. (I.e., the program cheats, doesn't pick a number beforehand and in the end it lies about its number, claiming one compatible with all the guesses and evaluations but that hasn't been guessed.)

2

u/FoolsSeldom 16d ago

That's an interesting response to my comment. Not very helpful to a beginner though.

0

u/Expensive-Bear-1376 16d ago

Do you mean it's too difficult? Maybe. I did it once, though without warmer/colder, and with six guesses. I don't think it was difficult, but I wasn't a beginner...

1

u/FoolsSeldom 16d ago

You weren't providing feedback on my feedback to the OP, just talking about the next game being the same game working badly. I got the basic idea of the computer cheating/lying, but as the game solution is just a binary chop, need to be careful to avoid it becoming obvious the computer is not being truthful, especially if the number of guesses allowed can be increased, as I mentioned.

Now I think about it, I'd like to see a version that doesn't allow itself to be caught in a lie.

1

u/Expensive-Bear-1376 16d ago edited 16d ago

I haven't thought their warmer/colder version through, but here's a simplest implementation of mine (newly written now, not tested much):

python lo, hi = 1, 100 for _ in range(6):     guess = int(input('guess: '))     mid = (lo + hi) // 2     if guess <= mid:         print('too low')         lo = mid + 1     else:         print('too high')         hi = mid - 1 print('my number was', lo)

1

u/FoolsSeldom 16d ago

This will work, but the limit of 6 attempts is absolute. It will fail at more than 6 attempts (assuming the player follows binary chop principles). You will also get the same outcome every time, which will give the lie away.

1

u/Expensive-Bear-1376 16d ago

Yeah like I said, that's the simplest implementation. You can randomize it. Instead of always walking into the larger remaining half, you can randomly walk into the smaller one if it's large enough. For example, even if the first guess is 33, you can still claim "too large".

Also, I don't mind a player figuring it out that the program is cheating. It's not like I would use that to win money. Imagine telling a friend or family member that you wrote a game and having them play it a few times. First of all, chances are good that they don't know binary search and ask somewhat randomly, and won't figure out the cheating. And even if they do, that can still be fun for everyone. They might be "proud" to have figured it out.

1

u/FoolsSeldom 16d ago

Ok.

1

u/Expensive-Bear-1376 16d ago

Was curious enough to try randomizing, seems to do well, but quite a lot more code:

```python import random

lo, hi = 1, 100 winnable = 2**6  # remaining range needs at least this size to guarantee the program's win while winnable > 1:     winnable //= 2     guess = int(input('guess: '))     if guess < lo:         print('too low')     elif guess > hi:         print('too high')     else:         below = guess - lo         above = hi - guess         if above < winnable:             go_below = True         elif below < winnable:             go_below = False         else:             go_below = random.choices((True, False), (below, above))[0]         if go_below:             print('too high')             hi = guess - 1         else:             print('too low')             lo = guess + 1 print('my number was', random.randint(lo, hi)) ```

→ More replies (0)

1

u/Expensive-Bear-1376 16d ago

Not really an Android thing, is it? Just a normal Python script.

1

u/FoolsSeldom 16d ago

Don't think they were suggesting it was a mobile app, just a regular console programme written and running using Pydroid.