PDA Assignments
  • Python For Data Analytics
    • 1.Python
      • 1.Python Documents
        • 1.Data Types
        • 2.Variables In Python
        • 3.Operators In Python
        • 4.User Input In Python
        • 5.TypeCasting In Python
        • 6.Strings In Python
        • 7.Conditional Statements In Python
        • 8.Branching using Conditional Statements and Loops in Python
        • 9.Lists In Python
        • 10.Sets In Python
        • 11.Tuples In Python
        • 12.Dictionary In Python
        • 13.Functions In Python
        • 14.File Handling In Python
        • 15.Numerical Computing with Python and Numpy
      • 2.Python Assignments
        • Data Type & Variables
        • Operators Assignment
        • User Input & Type Casting
        • Functions- Basic Assignments
        • String Assignments
          • String CheatSheet
        • Conditional Statements Assignments
        • Loops Assignments
        • List Assignments
          • List Cheatsheet
        • Set Assignments
          • Sets Cheatsheet
        • Dictionary Assignments
          • Dictionary Cheatsheet
        • Function Assignments
        • Functions used in Python
      • 3.Python Projects
        • Employee Management System
        • Hamming distance
        • Webscraping With Python
          • Introduction To Web Scraping
          • Importing Necessary Libraries
          • Basic Introduction To HTML
          • Introduction To BeautifulSoup
          • Flipkart Web Scraping
            • Scraping Step By Step
        • Retail Sales Analysis
        • Guess the Word Game
        • Data Collection Through APIs
        • To-Do List Manager
        • Atm-functionalities(nested if)
        • Distribution of Cards(List & Nested for)
        • Guess the Number Game
      • 4.Python + SQL Projects
        • Bookstore Management System
    • 2.Data Analytics
      • 1.Pandas
        • 1.Pandas Documents
          • 1.Introduction To Pandas
          • Reading and Loading Different Data
          • 2.Indexing and Slicing In Pandas
          • 3.Joining In Pandas
          • 4.Missing Values In Pandas
          • 5.Outliers In Pandas
          • 6.Aggregating Data
          • 7.DateTime In Pandas
          • 8.Validation In Pandas
          • 9.Fetching Data From SQL
          • 10. Automation In Pandas
          • 11.Matplotlib - Data Visualization
          • 12. Seaborn - Data Visualization
          • 13. Required Files
        • 3.Pandas Projects
          • Retail Sales Analysis
            • Retail Sales Step By Step
          • IMDB - Dataset Analysis - Basic
        • 2. Pandas Assignments
          • 1. Reading and Loading the Data
          • 2. Data frame Functions and Properties
          • 3. Series - Basic Operations
          • 4. Filtering in Pandas
          • 5. Advance Filtering
          • 6. Aggregate Functions & Groupby
          • 7. Pivot Tables
          • 8. Datetime
          • 9. String Functions
Powered by GitBook
On this page
  1. Python For Data Analytics
  2. 1.Python
  3. 3.Python Projects

Guess the Number Game

Create a game called 'Guess the number'. Let us go through the Rules one by one.

1. The computer will generate a Random Number.

2. Ask the user to guess the number in 10 chances.

3. If the user guesses it right, Score him accordingly like if the user guesses in the first chance, the score is 100, in second chance score should be 90, and so on.

  1. If the guess number is greater than the random number give him 'Hint: Choose a Lower Number' or less than the random number give him 'hint: Choose a Higher Number' or if the guess number is equal to random, No need to hint, just display score and end the game.

5. Also show how many chances are left.

6. if the user cannot guess the number, disclose the random number and end the game.

Example

Guess the number : 60

Hint : Choose a Lower Number

---------------Number of Chances Left -------------->1

Guess the number : 50

Hint : Choose a Higher Number.

---------------Number of Chances Left -------------->2

Guess the number : 55

Hint : Choose a Higher Number.

---------------Number of Chances Left -------------->3

Guess the number : 58

You Won .

Score : 70

Solution
import random
random_number = random.randint(1,100) 
print(random_number)
chances = 1 
score = 110 
while(chances<=10): 
    guess_number = int(input('Guess the number : ')) 
    if(guess_number==random_number): 
        score = score - chances*10 
        print(f'You Won . Score : {score}') 
        break 
elif(guess_number>random_number): 
    print('Hint : Choose a Lower Number') 
else: 
    print('Hint : Choose a Higher Number.') 
    print(f'---------------Number of Chances Left -------->{chances}') 
    chances = chances + 1 
else: 
    print(f'Sorry ! You lost . You ran out of your chances. Random number was : {random_number}')

PreviousDistribution of Cards(List & Nested for)Next4.Python + SQL Projects

Last updated 8 months ago