{"id":2166,"date":"2023-08-31T07:28:11","date_gmt":"2023-08-31T07:28:11","guid":{"rendered":"https:\/\/sahaldecode.com\/?p=2166"},"modified":"2023-08-31T07:43:31","modified_gmt":"2023-08-31T07:43:31","slug":"guess-the-number-python","status":"publish","type":"post","link":"https:\/\/sahaldecode.com\/guess-the-number-python\/","title":{"rendered":"Guess the Number Python: Python Bignner Projects"},"content":{"rendered":"\n

Both novice coders and experienced developers alike seek to challenge themselves through a diverse array of projects, aiming to enrich their skill sets and broaden their perspectives on multifaceted aspects of programming and innovative problem-solving. Among the endeavors frequently undertaken is the development of a ‘Guess the Number Python’ game\u2014a project that often ranks high on the priority list. This undertaking solely relies on a single Python module to accomplish its objectives. <\/p>\n\n\n\n

So, in \u201cGuess the Number Python<\/strong>\u201d we first have to import the\u00a0random module<\/a>\u00a0from Python\u2019s inbuilt libraries.<\/p>\n\n\n\n

<\/circle><\/circle><\/circle><\/g><\/svg><\/span><\/path><\/path><\/svg><\/span>
#Import Random Module<\/span><\/span>\nimport<\/span> random<\/span><\/span><\/code><\/pre>JavaScript<\/span><\/div>\n\n\n\n

<\/p>\n\n\n\n

This module will be used in selecting a number between a defined set or range. We now need to select a range between which the user is to guess a number. To do this, the user inputs a number; a Higher Range and a Lower Range. Define a function for the game.<\/strong><\/p>\n\n\n\n

<\/circle><\/circle><\/circle><\/g><\/svg><\/span><\/path><\/path><\/svg><\/span>
# Define a <\/span>function<\/span> <\/span>for<\/span> <\/span>the<\/span> <\/span>game<\/span><\/span>\ndef<\/span> <\/span>guess_the_number<\/span>()<\/span>:<\/span><\/span>\n    # <\/span>Generate<\/span> <\/span>a<\/span> <\/span>random<\/span> <\/span>number<\/span> <\/span>between<\/span> <\/span>1<\/span> <\/span>and<\/span> <\/span>100<\/span><\/span>\n    <\/span>target_number<\/span> = <\/span>random<\/span>.<\/span>randint<\/span>(1, 100)<\/span><\/span>\n    <\/span>attempts<\/span> = 0  # <\/span>Initialize<\/span> <\/span>the<\/span> <\/span>number<\/span> <\/span>of<\/span> <\/span>attempts<\/span><\/span><\/code><\/pre>JavaScript<\/span><\/div>\n\n\n\n

<\/p>\n\n\n\n

After getting a Higher Range and a Lower Range within which a random number will be selected, we pick a random number using a function from the random module.<\/p>\n\n\n\n

<\/circle><\/circle><\/circle><\/g><\/svg><\/span><\/path><\/path><\/svg><\/span>
 # Display a welcome message<\/span><\/span>\n    <\/span>print<\/span>(<\/span>"<\/span>Welcome to Guess the Number Python Game!<\/span>"<\/span>)<\/span><\/span>\n    <\/span>print<\/span>(<\/span>"<\/span>I'm thinking of a number between 1 and 100. Can you guess it?<\/span>"<\/span>)<\/span><\/span><\/code><\/pre>JavaScript<\/span><\/div>\n\n\n\n

<\/p>\n\n\n\n

The user is then assigned a number of chances to guess a number which will be a counter for a loop. We use a while loop since the user can get the right guess at any trial. The whole case will check if the user has guessed the right number and if so, exits the loop. If not the number of chances decreases by one until the user has no more chances and the random number is revealed to him.<\/p>\n\n\n\n

To ensure that the program runs continuously and doesn\u2019t close the program window, we add a true statement and print a line to separate the blocks.<\/p>\n\n\n\n

<\/circle><\/circle><\/circle><\/g><\/svg><\/span><\/path><\/path><\/svg><\/span>
# Start a loop for the game<\/span><\/span>\n    <\/span>while<\/span> True:<\/span><\/span>\n        <\/span>try<\/span>:<\/span><\/span>\n            # Get the player<\/span>'<\/span>s guess as an intege<\/span>r<\/span><\/span>\n            guess <\/span>=<\/span> <\/span>int<\/span>(<\/span>input<\/span>(<\/span>"<\/span>Enter your guess: <\/span>"<\/span>))<\/span><\/span>\n            attempts <\/span>+=<\/span> <\/span>1<\/span>  # Increment the attempts counter<\/span><\/span>\n<\/span>\n            # Compare the player<\/span>'<\/span>s guess with the target numbe<\/span>r<\/span><\/span>\n            <\/span>if<\/span> guess <\/span><<\/span> target_number:<\/span><\/span>\n                <\/span>print<\/span>(<\/span>"<\/span>Too low! Try again.<\/span>"<\/span>)<\/span><\/span>\n            elif guess <\/span>><\/span> target_number:<\/span><\/span>\n                <\/span>print<\/span>(<\/span>"<\/span>Too high! Try again.<\/span>"<\/span>)<\/span><\/span>\n            <\/span>else<\/span>:<\/span><\/span>\n                # Congratulate the player and display the number <\/span>of<\/span> attempts<\/span><\/span>\n                <\/span>print<\/span>(f<\/span>"<\/span>Congratulations! You've guessed the number {target_number} in {attempts} attempts.<\/span>"<\/span>)<\/span><\/span>\n                <\/span>break<\/span>  # Exit the loop <\/span>as<\/span> <\/span>the<\/span> <\/span>game<\/span> <\/span>is<\/span> <\/span>over<\/span><\/span>\n        except ValueError:<\/span><\/span>\n            <\/span>print<\/span>(<\/span>"<\/span>Invalid input. Please enter a valid number.<\/span>"<\/span>)<\/span><\/span><\/code><\/pre>JavaScript<\/span><\/div>\n\n\n\n

<\/p>\n\n\n\n

<\/p>\n\n\n\n

<\/circle><\/circle><\/circle><\/g><\/svg><\/span><\/path><\/path><\/svg><\/span>
# Import the random <\/span>module<\/span> <\/span>to<\/span> <\/span>generate<\/span> <\/span>random<\/span> <\/span>numbers<\/span><\/span>\nimport<\/span> random<\/span><\/span>\n<\/span>\n# Define a function for the game<\/span><\/span>\ndef guess_the_number():<\/span><\/span>\n    # Generate a random number between 1 and 100<\/span><\/span>\n    target_number = random.randint(1, 100)<\/span><\/span>\n    attempts = 0  # Initialize the number of attempts<\/span><\/span>\n<\/span>\n    # Display a welcome message<\/span><\/span>\n    print(<\/span>"<\/span>Welcome to Guess the Number Python Game!<\/span>"<\/span>)<\/span><\/span>\n    print(<\/span>"<\/span>I'm thinking of a number between 1 and 100. Can you guess it?<\/span>"<\/span>)<\/span><\/span>\n<\/span>\n    # Start a loop for the game<\/span><\/span>\n    while True:<\/span><\/span>\n        try:<\/span><\/span>\n            # Get the player<\/span>'<\/span>s guess as an intege<\/span>r<\/span><\/span>\n            guess = int(input(<\/span>"<\/span>Enter your guess: <\/span>"<\/span>))<\/span><\/span>\n            attempts += 1  # Increment the attempts counter<\/span><\/span>\n<\/span>\n            # Compare the player<\/span>'<\/span>s guess with the target numbe<\/span>r<\/span><\/span>\n            if guess < target_number:<\/span><\/span>\n                print(<\/span>"<\/span>Too low! Try again.<\/span>"<\/span>)<\/span><\/span>\n            elif guess > target_number:<\/span><\/span>\n                print(<\/span>"<\/span>Too high! Try again.<\/span>"<\/span>)<\/span><\/span>\n            else:<\/span><\/span>\n                # Congratulate the player and display the number of attempts<\/span><\/span>\n                print(f<\/span>"<\/span>Congratulations! You've guessed the number {target_number} in {attempts} attempts.<\/span>"<\/span>)<\/span><\/span>\n                break  # Exit the <\/span>loop<\/span> <\/span>as<\/span> the game is over<\/span><\/span>\n        except ValueError:<\/span><\/span>\n            print(<\/span>"<\/span>Invalid input. Please enter a valid number.<\/span>"<\/span>)<\/span><\/span>\n<\/span>\n    # Ask if the player wants to play again<\/span><\/span>\n    play_again = input(<\/span>"<\/span>Do you want to play again? (yes\/no): <\/span>"<\/span>)<\/span><\/span>\n    if play_again.lower() == <\/span>"<\/span>yes<\/span>"<\/span>:<\/span><\/span>\n        guess_the_number()  # Restart the game<\/span><\/span>\n    else:<\/span><\/span>\n        print(<\/span>"<\/span>Thank you for playing!<\/span>"<\/span>)<\/span><\/span>\n<\/span>\n# Start the game by calling the function<\/span><\/span>\nguess_the_number()<\/span><\/span>\n<\/span><\/code><\/pre>JavaScript<\/span><\/div>\n\n\n\n

<\/p>\n\n\n\n

Thanks for reading through Our article. Keep updated for new articles.<\/strong><\/p>\n","protected":false},"excerpt":{"rendered":"

Both novice coders and experienced developers alike seek to challenge themselves through a diverse array of projects, aiming to enrich … <\/p>\n

Read more<\/a><\/p>\n","protected":false},"author":1,"featured_media":2170,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"rank_math_lock_modified_date":false,"footnotes":""},"categories":[142,31],"tags":[143,145],"class_list":["post-2166","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-python-projects","category-projects","tag-guess-the-number-python","tag-sahal-decode","resize-featured-image"],"_links":{"self":[{"href":"https:\/\/sahaldecode.com\/wp-json\/wp\/v2\/posts\/2166"}],"collection":[{"href":"https:\/\/sahaldecode.com\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/sahaldecode.com\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/sahaldecode.com\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/sahaldecode.com\/wp-json\/wp\/v2\/comments?post=2166"}],"version-history":[{"count":0,"href":"https:\/\/sahaldecode.com\/wp-json\/wp\/v2\/posts\/2166\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/sahaldecode.com\/wp-json\/wp\/v2\/media\/2170"}],"wp:attachment":[{"href":"https:\/\/sahaldecode.com\/wp-json\/wp\/v2\/media?parent=2166"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/sahaldecode.com\/wp-json\/wp\/v2\/categories?post=2166"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/sahaldecode.com\/wp-json\/wp\/v2\/tags?post=2166"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}