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

Python : functional programming characteristics
Functions are first class (objects). That is, everything you can do with "data" can be done with functions themselves (such as passing a function to another function). Recursion is used as a primary...

Using Lambda and Filter with Regular expressions.
1) Start the Python interpreter and import the re module: >>> import re 2)Define a tuple strings to filter with regular expressions: t=('python','pycon','perl','peril','step','stop','come','came','exercise','hello','storm','my.mail','year','yam') ...

DBMS - SQL - Any/All
All - Operator SELECT empno, sal FROM emp WHERE sal > ALL (1999, 2999, 3999); Output of Above query is same as below query SELECT empno, sal FROM emp WHERE sal > 1999 AND sal > 2999...

Radhe Shyam

0 0
0


How to know if an object is iterable?
An object in python is said to be an iterable if it obeys one of the following two rules:- 1. Object must consist of __getitem__ method 2. Object must consist of __iter__ method. String objects generally...
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