Python tutorial for beginners – guessing game in python tutorial

Guessing game with python - python tutorials for begginer devpyjp.com

Python is a very simple and easy programming to learn and build things. Anyone can learn python from anywhere in the world. In this series of python tutorials for beginners, we going to create guessing games in python.

Learning a new technology is a very exciting journey, but you must enjoy your journey by exploring things in programming. Building simple games or programs can make your journey so much exciting.

In this guessing game in python tutorial, you need a simple basic python knowledge like if-elif-else and for loops are enough.

If you don’t know check out my python tutorial for beginners series or python tutorial videos in youtube.

Guessing game in python

This is a basic python tutorial , it will help you to understand the logic to build games in python programming. lets get started with so much excitement buddies.

Step -1

we need import python random function that can help us to generate random numbers in python. lets import that into your python file.

import random

Step -2

Now, greet your friend who is playing you game today. It is not necessary totally up to you and ask their name to greet them by using python input () functions.

print("Hello..! what's your name..?")
name = input()

Step -3

Now, generate a random number by using the python random function. this is our secret number which will guess by our friend in our guessing game in python. Here i am taking a range of 1 to 20. you can choose your own.

secret_number = random.randint(1,20)

Step – 4

Now, we going to create one for loop that can help us to restrict our friend guesses in minimal guesses. In our guessing game in python tutorial, we give 5 chances to our friend to guess our number.

And we use a simple if elif and else in python 3 to give hints to our friends to guess our number.

for i in range(1,6):
    print('Take a Guess:')
    guess = int(input())

    if guess > secret_number:
        print('Sorry..! your guess is too high ' +name+'.')
    elif guess < secret_number:
        print('Sorry..! your guess is too low '+name+'.')
    else:
        break

step -5

Now finally, if our friend guesses our number then congratulate them else give them a new trial for free by our guessing game in python program.

if guess == secret_number:
    print('Good job, '+name+'! you guessed my number in '+str(i)+'guesses.')

else:
    print('Nope, you exceeded guess times. My number is '+str(secret_number)+'.')

Congratulations! you did it man. you built a wonderful guessing game in python. For you the total code is here.

import random
print("Hello..! what's your name..?")
name = input()
print('well,'+name+ ' I am thinking of a number between range of 1 to 20..')
secret_number = random.randint(1,20)
for i in range(1,6):
    print('Take a Guess:')
    guess = int(input())
    if guess > secret_number:
        print('Sorry..! your guess is too high ' +name+'.')
    elif guess < secret_number:
        print('Sorry..! your guess is too low '+name+'.')
    else:
        break
if guess == secret_number:
    print('Good job, '+name+'! you guessed my number in '+str(i)+'guesses.')
else:
    print('Nope, you exceeded guess times. My number is '+str(secret_number)+'.')

I hope you definitely like python guessing game tutorial, check out our python tutorial for beginners here.

For more tutorials Please Subscribe to our newsletter, We are giving python tutorial PDF free download link especially to you. python tutorial PDF helps you to be a good programmer. Thank you.

Don't miss out!
Subscribe To Newsletter

Receive top Machine learning & Python news, lesson ideas, project tips, FREE Ebooks and more!

Invalid email address
Thanks for subscribing!

Leave a Reply