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

Build an interactive Dictionary using Python 3.x
I have downloaded a 5MB data base consisting of words and their meanings in a json file . I want to write a python progrsm which will ask the user to enter a word and then the program will search through...
B

Biswanath Banerjee

0 0
0

Use IPython shell for better understanding of python
Hi All, Even though there are many editors like IDLE, PyDev, PyCharm are available for python, it is very advantageous to use ipython as an interactive shell for Pyhon. How to install it? pip install...

Python in Unix
Python in Unix How to run command on unix Commands cannot be run directly on a unix server like we do in shell script. SYNTAX: system("Command") check_output("Command", shell=True) call("Command",...
R

Rishi B.

0 0
0

What is Python Egg?
(Attention: This is a technical post for Python developers. If you are not python programmer you might want to skip this). An .egg format files in Python is the standard distribution package contains...

Why Do People Choose Python?
Lot of programming languages are available in the market today, but why do people choose python over them? What made it different from other scirpting languages. So factors made it unique, as below: Software...
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