Create a Guessing Game That Your Friends Will Never Answer Correctly
Tags: python
Published: 2020-10-22 15:31:35

*Image by <a href="https://pixabay.com/users/geralt-9301/?utm_source=link-attribution&amp;utm_medium=referral&amp;utm_campaign=image&amp;utm_content=936724">Gerd Altmann</a> from <a href="https://pixabay.com/?utm_source=link-attribution&amp;utm_medium=referral&amp;utm_campaign=image&amp;utm_content=936724">Pixabay</a>*

If you work with Python, then at some point you’ve likely created a guessing program where you enter a number between 1 and 10. It’s fun, simple, and a great way to get you to learn how to use the random module and if/else statements.

But what if you were to design a guessing program where you (or your friends) **NEVER** get the correct answer? This isn't your typical [Python guessing game](https://dev.to/andemosa/building-a-guessing-game-with-python-g7m). No matter what you answer, the number you enter is wrong.

The best part about this is you'll never need to import a module for this. And it requires less than 20 lines of code, 10 if you feel ambitious.

```
answer = int(input("Enter a number between 1 and 10: "))
```

The first thing we enter for our code is the input. The variable answer is included and it's asking for a number between 1 and 10. We wrapped that input with an int() constructor to ensure that the number is an integer.

```
if answer == 10:
  print("I'm sorry, but the answer is 9.")
elif answer <= 0 or answer > 10:
  print("Sorry, but you need to enter a number between 1 and 10.")
else:
  answer = answer + 1
  print(f"I'm sorry, but the answer is {answer}. Please try again!")
```
From there, we create an if/else statement. We want to eliminate all of the possibilities first. So we start with the number 10. For that, we just added the print statement to say the correct answer is 9.

Next up is our else if or elif answer. And that is meant to eliminate anyone who wants to guess 0 or 11 or just about most other numbers. For that, we included the print statement reminding the player to enter a number between 1 and 10.

Finally, there is the else part of the statement. It's for the numbers 1 thru 9. And first, we update the variable to enter the answer + 1. That means if you enter 4, you get 5. If you enter 8, you get 9. And then we include the print statement.

`print(f"I'm sorry, but the answer is {answer}. Please try again!")`

You might notice the "f" before the actual statement. That's a part of the f-string. It's a [simpler way to format](https://realpython.com/python-f-strings/#python-f-strings-the-pesky-details) and if you use Python 3.6 on up, then you have this capability. I love the f-string, so this is a no brainer for me.

We could stop there. But I want to make this game something that keeps going and going and going.

```
def guesser():
  answer = int(input("Enter a number between 1 and 10: "))
  if answer == 10:
    print("I'm sorry, but the answer is 9.")
  elif answer <= 0 or answer > 10:
    print("Sorry, but you need to enter a number between 1 and 10.")
  else:
    answer = answer + 1
    print(f"I'm sorry, but the answer is {answer}. Please try again!")

while True:
  guesser()
```
To put it succinctly, we wrapped everything we previously wrote into a function and then added in the while loop.

It's that simple to write. You can have your friends run the program on their computer and watch them get frustrated at not getting the answer right. And they (or you) can play it for as long as they (or you) want.