Welcome!

By registering with us, you'll be able to discuss, share and private message with other members of our community.

SignUp Now!
  • Guest, before posting your code please take these rules into consideration:
    • It is required to use our BBCode feature to display your code. While within the editor click < / > or >_ and place your code within the BB Code prompt. This helps others with finding a solution by making it easier to read and easier to copy.
    • You can also use markdown to share your code. When using markdown your code will be automatically converted to BBCode. For help with markdown check out the markdown guide.
    • Don't share a wall of code. All we want is the problem area, the code related to your issue.


    To learn more about how to use our BBCode feature, please click here.

    Thank you, Code Forum.

Python How to make a guessing game in python

James

Active Coder
Okay, so in this tutorial, I will be going through the steps to create a guessing game in python 3.7

First, you will need to import the random module, this module is auto-installed along with python.
import random

Second, we need to greet the user when they join
Python:
print("""
Welcome to this guessing game in python!
Good luck!
""")

now we need to define a variables
Python:
points = 0
This variable will track your points throughout the game.

Okay, so now we need to make the actual game, to do this and make the game go on forever we need to make a loop.
Python:
while True:
    pass
Now we need to ask a random question
Python:
between = random.randint(10,25)
rn = random.randint(1,between)
question = input("I choose a number between 1 and "+str(between)+"\n>>> ")
Now let's see if they guessed right
Python:
if int(question) == between:
    points += 1
Still, the game goes on forever so lets add a loosing.
Python:
else:
    print("You lost!")
    print("You got "+points+" points")
    input(" ")
To put it all together,
Python:
import random
points = 0
print("""
Welcome to this guessing game.
Good luck!
""")
while True:
    between = random.randint(10,25)
    rn = random.randint(1,between)
    question = input("I choose a random number between 1 and "+str(between)+"\n>>> ")
    if int(question) == rn:
        points += 1
    else:
        print("You lost")
        print("You got "+str(points)+" points!")
        input(" ")
 
Last edited:
Just to let you know, the ending code block is correct, but you have a code block that says if int(question) == between...., but that should be if int(question) == rn, like it is in the end :)
 

New Threads

Latest posts

Buy us a coffee!

Back
Top Bottom