UrbanPro
true

Learn Python Training from the Best Tutors

  • Affordable fees
  • 1-1 or Group class
  • Flexible Timings
  • Verified Tutors

Search in

Using the random module: another program.

Ashish K Sharma
21/06/2022 0 0

A Python module is just a file that contains reusable code like functions. If a program is going to use functions that are stored in another module, it needs to first import that module.
The random module is one of the several modules available in python. It has functions for generating random numbers. The three functions associated with this module are:
1. random()---- Returns a random float value that is greater than or equal to 0.0 and less than 1.0.
2. randint(min,max)--- returns a random int value that is greater than or equal to the min argument and less than or equal to the maxargument.
3. randrange([start,],stop [,step]) --- Returns a random value greater than or equal to the start argument,less than the stop argument and a multiple of the step argument.

Here, we will write a program to illustrate its use. I have submitted earlier a program illustrating the use of this module(dieroll program) and again we write yet another program to illustrate its usage.
                                       GUESS THE NUMBER GAME.
Task: To implement a game in which the user guesses a number generated randomly by the
computer. If the user guesses correctly, then a message is displayed informing the user
the number of guesses it took for him/her to guess correctly and the program ends. If
the guess is too high or low, than the user is given another chance to guess. And,if the
user is unable to guess correctly after 6 attempts, the program informs the user the
correct number and then ends.

                                                       THE PROGRAM.
import os
import sys
import random
print("Let us play a game. I will pick a number")
print("between 1 and 100 and you try to guess it")
compnum = random.randint(1,100)
guesscount = 0
guess = int(input("Your guess"))
while True:
     guesscount = guesscount + 1
     if (guess == compnum):
         print("You got it in "+str(guesscount)+" guesses! My number was "+compnum)
         break

     if (guesscount == 6):
         print("You did not get the number in 6 Guesses. My number is"+str(compnum))
         break

     if (guess < compnum):
       print("That is too low. Try again")
       guess = int(input("Your guess"))

     elif (guess > compnum):
       print("That is too high.Try again")


     guess = int(input("Your guess"))

0 Dislike
Follow 1

Please Enter a comment

Submit

Other Lessons for You

Multiple Decorators
#Multiple decorators can be chained in Python.#A function can be decorated multiple times with different (or same) decorators.#We simply place the decorators above the desired function. def star(func):...

Decorator in Python - Simplified
By definition, a decorator is a function that takes another function andextends(/decorates) the behaviour of the latter function without explicitly modifying it.Let see how we can apply decorators for...

Be A Python Dev
Currently, in the programming world, Python is one of the languages with a high rising demand profile. And this article will explain why that isn’t slowing down in the foreseeable future.Python...

Python/Dejango Training
Python/Django Training Content Duration: 2.5 months 1. Introduction to Python Python - The Universal Language 2. Getting Started Installing Python Python - *Hello World* Using the Interpreter Python...

Identity operators
Identity operators Operation Syntax Function Identity a is b is_(a, b) Membership operators Operation Syntax Function Containment obj in seq contains(seq, obj)
X

Looking for Python Training Classes?

The best tutors for Python Training Classes are on UrbanPro

  • Select the best Tutor
  • Book & Attend a Free Demo
  • Pay and start Learning

Learn Python Training with the Best Tutors

The best Tutors for Python Training Classes are on UrbanPro

This website uses cookies

We use cookies to improve user experience. Choose what cookies you allow us to use. You can read more about our Cookie Policy in our Privacy Policy

Accept All
Decline All

UrbanPro.com is India's largest network of most trusted tutors and institutes. Over 55 lakh students rely on UrbanPro.com, to fulfill their learning requirements across 1,000+ categories. Using UrbanPro.com, parents, and students can compare multiple Tutors and Institutes and choose the one that best suits their requirements. More than 7.5 lakh verified Tutors and Institutes are helping millions of students every day and growing their tutoring business on UrbanPro.com. Whether you are looking for a tutor to learn mathematics, a German language trainer to brush up your German language skills or an institute to upgrade your IT skills, we have got the best selection of Tutors and Training Institutes for you. Read more