This content originally appeared on DEV Community and was authored by Code_Jedi
Hey everyone?, in this beginner tutorial, I'll show how you can make a super simple number guessing game in python.
Let's get started!
First, you will need to require the "random" library in order to generate random numbers:
import random
Next, print the conditions of the game at the beginning:
import random
print("Pick a number from 1 to 9")
Then define the function in which you will be writing your main code:
import random
print("Pick a number from 1 to 9")
def game():
The first things you'll need to put in this function are:
import random
print("Pick a number from 1 to 9")
def game():
random_number = random.randrange(1, 10)
typed_number = int(input("Type your number here: "))
print(random_number)
Using the "randrange()" function, you will randomly generate a number ranging from 1 to 9, then you will ask the player to type a number using the "input()" function and finally print the randomly generated number to let the player compare the random number with his/her typed number.
The final part of your function should look like this:
int_rand = int(random_number)
if (int_rand == typed_number):
print("You win!!!")
else:
print("Try again")
Let me explain each line of code:
- 1. "int_rand" will convert "random_number" into an integer using the "int()" function.
- 2, 3, 4. This will compare "int_rand" with "typed_number", if both numbers are equal to each other, "You win!!!" will be printed on the screen.
- 5, 6. if both numbers aren't equal to each other, "Try again" will be printed on the screen.
At this point, your code should look something like this:
import random
print("Pick a number from 1 to 9")
def game():
random_number = random.randrange(1, 10)
typed_number = int(input("Type your number here: "))
print(random_number)
int_rand = int(random_number)
if (int_rand == typed_number):
print("You win!!!")
else:
print("Try again")
Lastly, you need to put this function in an infinite while loop so that the game won't stop until the player guesses correctly, you will also need to put a "break" function inside the if statement so that once the player guessed correctly, the function would break out of the while loop, therefor stopping the guessing game, and as a last step, put the "game()" function at the end of the file to execute the function:
import random
print("Pick a number from 1 to 9")
def game():
while 1 < 100:
random_number = random.randrange(1, 10)
typed_number = int(input("Type your number here: "))
print(random_number)
int_rand = int(random_number)
if (int_rand == typed_number):
print("You win!!!")
break
else:
print("Try again")
game()
That's it for this beginner python tutorial, if you have any problems with the code, let me know in the comments.
Byeeeeee?
This content originally appeared on DEV Community and was authored by Code_Jedi
Code_Jedi | Sciencx (2021-07-10T11:45:27+00:00) Create a simple number guessing game in python. Retrieved from https://www.scien.cx/2021/07/10/create-a-simple-number-guessing-game-in-python/
Please log in to upload a file.
There are no updates yet.
Click the Upload button above to add an update.