loading
Hey everyone!
Since my last Python 3 Instuctable did really well and got featured, I thought I would show everyone how to code a fun guessing game in Python! This is my own modified version of a guessing game by Hywel Carver in the book 'Coding Unlocked'.
The program generates a random number and asks you for a number. If the number you choose is lower than the target number, the program will tell you to go a bit higher and ask you to choose again, and vice versa.
When your guess is correct, the program congratulates you and asks if you want to play again.
This program is more fun than my other one (which was intended for educational use anyway). You'll probably want to keep playing this game if you like guessing games.
Let's get right into it!

Step 1: Installing Python

I am not going to explain how to download Python here, as I have already in another Instructable. If you do not have Python already installed, go here and look for the 'Installing Python' step: https://www.instructables.com/id/Simple-Remainder-Calculator-in-Python-3
You should also read how to open a file for editing.

Step 2: Importing

This is simple. Type in 'import random' at the top of the file. There is no indentation.

Step 3: Creating a Funtion

On the next line, type:
def guessinggame():

There is no indentation.

Step 4: Programming the Function

Type these on the next lines (remember 4 spaces at the start):
target = random.randint
running = True

Step 5: Looping

This part will loop and ask the user for a number:
while running:
guess = int(input("Guess an integer between 0 and 100: "))

The first line has 4 spaces at the start, and the second line has 8.

Step 6: Conditions Part 1

This code will decide if you have guessed the correct number. I'm going to do this in a big chunk:
if guess == target:
running = False
again = input("Correct! Play again? (y/n) ")
if again == "y" or "Y":
guessinggame()
if again == "n" or "N":
print("Okay. See you next time!")
break

The first line has 8 spaces at the start.
The 2nd, 3rd, 4th and 6th line have 12 spaces.
The 5th, 7th and 8th line have 16 spaces at the start.

Step 7: Conditions Part 2

This code will tell the user if he needs to go higher:
if guess < target:
print("Higher!")

The first line has 8 spaces at the start and the second line has 12.

Step 8: Conditions Part 3

The last part of conditions. This code tests if the user needs to go lower:
if guess > target:
print("Lower!")

The 1st line has 8 spaces at the start of the line and the 2nd has 12.

Step 9: Error Handling (optional)

This code is pretty much the same as the remainder calculator's:
try:
guessinggame()
except:
print("Hey, that's not a number!")
guessinggame()

There are no spaces before try and except. There are four spaces before the 2nd, 4th and 5th lines.

Step 10: }{}{ If You Skipped the Last Step }{}{

All you need to do is type this on a new line:
guessinggame()

Step 11: Conclusion

Run the program.
You should be prompted to guess a number. Type in a number and press enter. If you get it wrong, the program will tell you if you need to type in a higher number or a lower number. If you get it right, you can choose to play again or exit. If there are any errors, please leave a comment or email me at redking9132@gmail.com. Also leave a comment or email if you have an idea you want me to do.
NB: There may be a difference in the code between the pictures and the text for the Instructable Any way should work fine.

RedKing9132 out!
<p>It's a nice and easy little game to write. I did change the questions to dutch. Thank you for sharing.</p>
Glad you liked it zifron!

About This Instructable

542views

9favorites

License:

Bio: Hi! I'm RedKing9132! I am 12 years old. I am into gaming, coding, 3D design, 3D pictures and 3D animation. Visit my blog: redking9132 ...
More by RedKing9132:Fun Python 3 Guessing Game Simple Remainder Calculator in Python 3 Write Your Name in 2-Point Perspective 
Add instructable to: