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.
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:
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.
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.
import random guessesUsed = 0 Name=input(‘Hello! What is your name? ‘) number = random.randint(1, 30) print(‘Greetings, ‘ + Name + ‘, I\’m thinking of a number between 1 and 30.’) while guessesUsed < 5: guess=int(input(‘Guess the number within 5 guesses...’)) guessesUsed = guessesUsed + 1 if guess < number: print(‘Too low, try again.’) if guess > number: print(‘Too high, try again.’) if guess == number: break if guess == number: guessesUsed = str(guessesUsed) print(‘Well done, ‘ + Name + ‘! You guessed correctly in ‘ + guessesUsed + ‘ guesses.’) if guess != number: number = str(number) print(‘Sorry, out of guesses. The number I was thinking of is ‘ + number)
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:
Endurance=0 CR=0 Luck=0 Endurance = random.randint(1, 15) CR = random.randint(1, 20) Luck = random.randint(1, 10) Print(“Your character’s stats are as follows:\n”) Print(“Endurance:”, Endurance) Print(“Combat Rating:”, CR) 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.
0 Response to "Number Guessing Game on Python"
Post a Comment