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- Basic Concepts part 2
1. Other Numerical Operations a. Exponentiation Besides addition, subtraction, multiplication, and division, Python also supports exponentiation, which is the raising of one number to the power of...

Datetime Module
#this examples demonstrate use of datetime module import datetimeob=datetime.datetime.now()print("-"*25)print(ob)print(ob.year)print(ob.month)print(ob.day)print(ob.hour)print(ob.minute)print(ob.second)str1=str(ob.hour)+':'+str(ob.minute)+':'+str(ob.second)print...

Built-In Functions (Python)
Built-in Functions: The Python interpreter has a number of functions built into it that are always available. They are listed here in alphabetical order. Built-in Functions abs() divmod() input() open() staticmethod() all() enumerate() int() ord() str() any() eval() isinstance() pow() sum() basestring() execfile() issubclass() print() super() bin() file() iter() property() tuple() bool() filter() len() range() type() bytearray() float() list() raw_input() unichr() callable() format() locals() reduce() unicode() chr() frozenset() long() reload() vars() classmethod() getattr() map() repr() xrange() cmp() globals() max() reversed() zip() compile() hasattr() memoryview() round() __import__() complex() hash() min() set() ...

Python Introduction
Python is an object-oriented programming language created by Rossum in 1989. It is ideally designed for rapid prototyping of complex applications. It has interfaces to many OS system calls and libraries...
I

Imran

0 0
0

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