-->

Number Guessing Game on Python

This is a simple little piece of code but it makes good use of the Random module, print and input,
and a while loop. The number of guesses can be increased from 5 and the random number range can easily be altered too.



NUMBERGUESS.PY

Copy the code and see if you can beat the computer within five guesses. It’s an interesting bit of code that can be quite handy when your implementing a combination of the Random module alongside a while loop.


  1. import random
  2. guessesUsed = 0
  3. Name=input(‘Hello! What is your name? ‘)
  4. number = random.randint(1, 30)
  5. print(‘Greetings, ‘ + Name + ‘, I\’m thinking of a
  6. number between 1 and 30.’)
  7. while guessesUsed < 5:
  8.     guess=int(input(‘Guess the number within 5 guesses...’))
  9.     guessesUsed = guessesUsed + 1
  10.     if guess < number:
  11.         print(‘Too low, try again.’)
  12.     if guess > number:
  13.         print(‘Too high, try again.’)
  14.     if guess == number:
  15.         break
  16.     if guess == number:
  17.         guessesUsed = str(guessesUsed)
  18.         print(‘Well done, ‘ + Name + ‘! You guessed
  19.         correctly in ‘ + guessesUsed + ‘ guesses.’)
  20.     if guess != number:
  21.         number = str(number)
  22.         print(‘Sorry, out of guesses. The number I was
  23.         thinking of is ‘ + number)


Number Guessing Game on Python
Number Guessing Game on Python


Number Guessing Game on Python


Number Guessing Game on Python


Code Improvements

Since this is such as simple script to apply to a situation, there’s plenty of room to mess around with it and make it more interesting. Perhaps you can include an option to take score, the best out of three rounds. Maybe an elaborate way to congratulate the player for getting a ‘hole in one’ correct guess on their first try.

Moreover, the number guessing game code does offer some room for implementing into your code in a different manner. What we mean by this is, the code can be used to retrieve a random number between a range, which in turn can give you the start of a character creation defined function within an adventure game.

Imagine the start of a text adventure written in Python, where the player names their character. The next step is to roll the virtual random dice to decide what that character’s combat rating, strength, endurance and luck values are. These can then be carried forward into the game under a set of variables that can be reduced or increased depending on the circumstances the player’s character ends up in.

For example, as per the screenshot provided, you could use something along the lines of:

  1. Endurance=0
  2. CR=0
  3. Luck=0
  4. Endurance = random.randint(1, 15)
  5. CR = random.randint(1, 20)
  6. Luck = random.randint(1, 10)
  7. Print(“Your character’s stats are as follows:\n”)
  8. Print(“Endurance:”, Endurance)
  9. Print(“Combat Rating:”, CR)
  10. Print(“Luck:”, Luck)

The player can then decide to either stick with their roll or try again for the hope of better values being picked. There’s ample ways in which to implement this code into a basic adventure game.

Number Guessing Game on Python



0 Response to "Number Guessing Game on Python"

Post a Comment

Iklan Atas Artikel

Iklan Tengah Artikel 1

Iklan Tengah Artikel 2

Iklan Bawah Artikel