Coding My First Game In Python!



So, if you already know how to code in Python, this may seem extremely mundane and simple, but I made a little game in class! This game is a number guessing game, where the computer will randomly generate a number and you have to guess what number it is thinking of. The computer will let you know whether your guess is too high or too low, and it continues to run until you find the correct number. For me, that is super exciting because it is the first interactive game on Python I made, that you can code too! I'll admit that this took me less than 5 minutes to code, even with the fact that I originally made a stupid mistake, but I'll quickly walk you through what I did.

First and foremost, I needed to import random, as this will allow the computer to randomly generate a number between a range that I assign it. In this case, I gave it a range of 1 to 100. In order to tell the computer that you want a randomly generated number you need to use random.randit(a,b), where a and b are the limits of your range. I just named it num for number.

To say that you want the user to input a number, you're going to use input("Your text here").  Prior to that, I added nb = int to the input. This allows me to have a distinguished name for the input (nb) and int means that it only accepts integers. Also, when you add your text in input, I recommend adding \n before the closing quotation marks so the user will input their guess on the next line and not the same as the text.

The final chunk of code left is the conditional loop while True:. Just like you can probably guess by the name, while True will continue to run the code and act in a loop until the condition is proven true. In this case, until you find the correct number. Within this conditional loop, you have more conditions indicated by if, elif, and else. The if is pretty straight forward. If the number inputted is the same as the randomly generated number, then it will tell you that you are correct. However if that is not the case, then it will try the elif condition. The elif condition states that if the number inputted is less than the number generated, then the number you have entered is too small, and you will have to try again. This is actually where I made my only mistake. I forgot to redefine the input with the nb=int and since I didn't do that, it wouldn't register the new guess I would make. That made it stuck in a loop where every guess I made was not the correct number... But now it's fixed so please remember to define your input. Finally, there is the else condition. Since the number you guessed what not correct and too small, the only thing it can be is too big, so you would have to try again.

There you go! As simple as that! You have programmed your first game and you can now show it off to all your friends and family!




If you want to try this out for yourself, but don't have Visual Studio code, you can simply copy my code below and paste it into this website: https://repl.it/languages/python3 and hit run.

import random 

num=random.randint(1,100)

nb = int(input("Guess what number I am thinking of between 1 to 100 \n"))

while True:
   
    if nb==num:
        print("Correct!")
        break
    elif nb<num:
        nb = int(input("The number is bigger, try again \n"))
    else:
        nb = int(input("The number is smaller, try again \n")) 

Comments

Popular Posts